标签: partial-specialization

在成员函数返回类型上参数化的类模板部分特化

以下代码尝试根据成员函数指针类型的返回类型来专门化类模板'special',导致VC9编译错误:

template<class F> struct special {};
template<class C> struct special<void(C::*)()> {};
template<class R, class C> struct special<R(C::*)()> {};

struct s {};

int main()
{
  special<void(s::*)()> instance;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误C2752:'special':多个部分特化匹配模板参数列表

GCC-4.3.4接受相同的代码,如下所示:http ://ideone.com/ekWGg
这是VC9中的一个错误,如果是这样,这个错误是否仍然存在于VC10中?

然而,我提出了一个可怕的侵入式解决方法(对于这个特定的用例,至少.欢迎更一般的解决方案):

#include <boost/function_types/result_type.hpp>
#include <boost/type_traits/is_same.hpp>

template<typename F, typename R>
struct is_result_same :
  boost::is_same<
    typename boost::function_types::result_type<F>::type,
    R
  >
{};

template<class F, bool = is_result_same<F, void>::value>
struct special {};

template<class R, class C> struct special<R(C::*)(), true>  {};
template<class R, class C> struct special<R(C::*)(), false> {};
Run Code Online (Sandbox Code Playgroud)

c++ templates member-function-pointers partial-specialization visual-c++

6
推荐指数
1
解决办法
1279
查看次数

使用C++中的模板展开循环,并进行部分特化

我正在尝试使用模板在C++中展开循环,如下所示.

#include <iostream>

template< class T, T i >
struct printDown {
    static void run(void) {
        std::cout << i << "\n";
        printDown< T, i - 1 >::run();
    }
};

template< class T >
struct printDown< T, 0 > {
    static void run(void) {
        std::cout << 0 << "\n";
    }
};

int main(void) {
    printDown< int, 10 >::run();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在Cygwin中编译w/g ++ 3.4.4时,我收到以下错误.

tmp.cpp:12:错误:类型T' of template argument0'取决于模板参数

我究竟做错了什么?我是否需要以某种方式注释0来说它是T型?

提前致谢.

c++ templates partial-specialization specialization loop-unrolling

6
推荐指数
1
解决办法
475
查看次数

Sun Studio 12中的模板编译错误

我们正在迁移到Sun Studio 12.1并使用新的编译器[CC:Sun C++ 5.10 SunOS_sparc 2009/06/03].编译使用早期版本的Sun编译器[CC:Sun WorkShop 6 update 2 C++ 5.3 2001/05/15]编译好的代码时,我遇到了编译错误.

这是我得到的编译错误.

"Sample.cc":错误:无法找到main()中所需的LoopThrough(int [2])匹配项.1检测到错误.***错误代码1.

码:

#include <iostream> 

#define PRINT_TRACE(STR) \
std::cout << __FILE__ << ":" << __LINE__ << ":" << STR << "\n";

template<size_t SZ>
void LoopThrough(const int(&Item)[SZ])
{
    PRINT_TRACE("Specialized version");
    for (size_t index = 0; index < SZ; ++index)
    {
        std::cout << Item[index] << "\n";
    }
}


/*     
    template<typename Type, size_t SZ>
    void LoopThrough(const Type(&Item)[SZ])
    {
        PRINT_TRACE("Generic version");        
    }
 */  



int main()
{
    {
       int arr[] …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization sunstudio overload-resolution

6
推荐指数
1
解决办法
890
查看次数

特定值的C++模板特化

我已经结构Opers与算术操作:mult(),div(),mod().

我需要专门为某些值的模板n.这是一个例子Opers<1>.

但是,我也想做专门化,n因为2的幂(n = 2,4,8,16,...) - 在这种情况下,我可以优化操作mult()div()(使用左或右按位移位).

#include <iostream>
using namespace std;
template<int n> struct Opers {
    int mult(int x){
        return n*x;
    }
    int div(int x){
        return x / n;
    }   
    int mod(int x){
        return x % n;
    }   
};
template<> struct Opers<1> {
    int mult(int x){
        return 1;
    }
    int div(int x){
        return x;
    }   
    int mod(int x){
        return 0;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization template-specialization c++11

6
推荐指数
1
解决办法
1187
查看次数

C++:用于检查表达式是否编译的模板

在使用SFINAE编写模板专业化时,您经常会因为一个小的不存在的成员或函数而需要编写一个全新的特化.我想把这个选择打包成一个小的声明,比如orElse<T a,T b>.

小例子:

template<typename T> int get(T& v){
    return orElse<v.get(),0>();
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

c++ templates partial-specialization specialization sfinae

6
推荐指数
1
解决办法
445
查看次数

多种类型的部分类模板专业化

我有一个类,允许创建包含任何类型或类的向量。不过,我想为数字类型添加附加功能。

template <>
class Vec<double> : public VecBase<double>
{
    //  == METHODS ==
    public:
        //  -- Constructors & Destructors --
        explicit Vec(const unsigned long long t_size);
        virtual ~Vec();

        //  -- Operators --
        friend Vec<double> operator+(const Vec<double>&, const double);

        //  -- Methods --
        double sum();

... etc.
Run Code Online (Sandbox Code Playgroud)

我已经部分专门化了类模板,以允许重载数学运算符以实现双重专门化。我现在也想将此专业化扩展到 int ,但不是复制专业化以 int 替换 double ,有没有办法将其添加到专业化列表中?

也就是说,有没有办法允许:

template<>
class Vec<double (or) int>
Run Code Online (Sandbox Code Playgroud)

干杯!

c++ templates operator-overloading partial-specialization template-specialization

6
推荐指数
1
解决办法
2441
查看次数

部分专业化时基于C++模板的"覆盖"等效?

我有一个模板类/结构,如下所示:

template <typename T, typename U>
struct S
{
    unsigned int operator()(T t, U u) const;
};
Run Code Online (Sandbox Code Playgroud)

我想确保专业化尊重这个界面.

不幸的是,我可以使用不同的返回类型来专门化这个结构.例如,如果我部分专门返回bool而不是unsigned int,我希望得到编译器错误,但编译器似乎并不关心:

template <typename T>
struct S<T,nullptr_t>
{
    bool operator()(T t, nullptr_t u) const { return 2; }
};
Run Code Online (Sandbox Code Playgroud)

示例 @ Ideone.com

在上面的示例中,应该返回专用版本,2但是由于返回类型是bool,返回值将被转换true为然后显示为1.

为什么编译器会接受这个?

如何防止程序员使用错误的返回类型(甚至错误的参数)专门化模板?

我知道我可以在基本模板类/结构中使用虚方法实现我想要的,并override在子代中使用关键字:

使用覆盖的解决方案(不编译,这很好)

但是拥有一个虚拟方法肯定会创建一个虚拟表,我想尽可能地避免这种情况,特别是因为我在运行时不需要虚拟表.除非有一个技巧可以在不构建虚拟表的情况下做同样的事情?

另外,我知道问题会更简单,如果我可以部分专门化方法或者我可以依赖非部分专业化,但前者在C++ AFAIK中是不可能的,后者不包括我在程序中需要的情况.

c++ templates overriding partial-specialization

6
推荐指数
1
解决办法
275
查看次数

变量模板的部分专业化

我知道我可以部分专门化类模板,我知道我不能部分指定函数模板.

变量模板怎么样?我找不到关于它们是否可以部分专业化的文档.

c++ templates partial-specialization language-lawyer variable-templates

6
推荐指数
1
解决办法
416
查看次数

如何在 C++ 中的类体之外定义一个专门的类方法?

我有一个模板类A<T>及其对整数参数的专业化。并且类及其专业化都声明了方法foo(),我想在类主体之外定义它:

#include <concepts>

template<class T> 
struct A { static void foo(); };

template<std::integral T>
struct A<T> { static void foo(); };

template<class T>
void A<T>::foo() {}

template<std::integral T>
void A<T>::foo() {}
 
int main() { A<int>::foo(); }
Run Code Online (Sandbox Code Playgroud)

GCC 接受此代码。

Clang 打印错误https://gcc.godbolt.org/z/hYfYGPfMh

error: type constraint differs in template redeclaration
template<std::integral T>
Run Code Online (Sandbox Code Playgroud)

MSVC 会在两个方法定义上打印错误:

error C3855: 'A<T>': template parameter 'T' is incompatible with the declaration
error C2447: '{': missing function header (old-style formal list?)
error C2065: 'foo': …
Run Code Online (Sandbox Code Playgroud)

c++ partial-specialization c++-concepts c++20

6
推荐指数
1
解决办法
157
查看次数

这个部分类专业化是有问题的还是 clang 或 gcc 中的错误?

template <class A>
struct Foo {
  template <class Bar>
  constexpr auto a_method();
};

template <class A>
template <class Bar>
constexpr auto Foo<A>::a_method() {
    return 42;
}

template <>
template <class Bar>
constexpr auto Foo<void>::a_method() {
    return 42;
}
Run Code Online (Sandbox Code Playgroud)

GCC可以编译这个。

但铿锵不能。错误输出:

<source>:15:27: error: conflicting types for 'a_method'
constexpr auto Foo<void>::a_method() {
                          ^
<source>:4:18: note: previous declaration is here
  constexpr auto a_method();
                 ^
1 error generated.
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)

c++ gcc partial-specialization clang

6
推荐指数
1
解决办法
283
查看次数