标签: specialization

C++模板类专业化:为什么需要重新实现常用方法

在样本中:

#include <iostream>

using namespace std;

class B
{
public:
    virtual void pvf() = 0;
};

template <class T>
class D : public B
{
public:
    D(){}

    virtual void pvf() {}

private:
    string data;
};

template <>
class D<bool> : public B
{
public:
    D();

    virtual void pvf(){ cout << "bool type" << endl; }
};

int main()
{
    D<int> d1;
    D<bool> d2;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

test.cpp:(.text+0x1c): undefined reference to `D<bool>::D()'
Run Code Online (Sandbox Code Playgroud)

请注意,我不仅仅专门研究D()的原因是我想D<T>::data在这种D<bool>情况下消除字符串的需要.

为什么我需要重新实现D()D<bool>?好像应该有一种方法让我告诉编译器使用该版本 …

c++ templates specialization

14
推荐指数
3
解决办法
9454
查看次数

部分模板专业化模糊

我不明白为什么主要陈述含糊不清.

template<class T, class U, int I> struct X
{ void f() { cout << "Primary template" << endl; } };


template<class T, int I> struct X<T, T*, I>
{void f() { cout << "Partial specialization 1" << endl;}};

template<class T, class U, int I> struct X<T*, U, I>
{void f() { cout << "Partial specialization 2" << endl;}};

template<class T> struct X<int, T*, 10>
{void f() { cout << "Partial specialization 3" << endl;}};

template<class T, class U, int …
Run Code Online (Sandbox Code Playgroud)

c++ templates partial-specialization specialization template-specialization

13
推荐指数
1
解决办法
1768
查看次数

多个类的一个模板专门化

假设我们有一个模板函数"foo":

template<class T>
void foo(T arg)
{ ... }
Run Code Online (Sandbox Code Playgroud)

我可以针对某些特定类型进行专业化,例如

template<>
void foo(int arg)
{ ... }
Run Code Online (Sandbox Code Playgroud)

如果我想对所有内置数值类型(int,float,double等)使用相同的特化,我会多次写这些行.我知道身体可以被扔到另一个功能,并且只是在每个专业的身体中调用它,但是如果我能避免为每种类型写出这个"void foo(...")会更好.是否有有没有可能告诉编译器我想对所有这些类型使用这个特化?

c++ templates specialization

12
推荐指数
1
解决办法
3367
查看次数

专用功能模板,用于参考类型

为什么输出这段代码:

#include <iostream>  
template<typename T> void f(T param) 
{ 
   std::cout << "General" << std::endl ; 
} 
template<> void f(int& param) 
{ 
   std::cout << "int&" << std::endl ; 
}  

int main() 
{   
  float x ;  f (x) ;   
  int y ; f (y) ;   
  int& z = y ; f (z) ; 
}  
Run Code Online (Sandbox Code Playgroud)

一般
一般
一般

第三个是令人惊讶的,因为该功能完全专门用于 int&

编辑:我知道重载可能是一个合适的解决方案.我只想学习它背后的逻辑.

c++ templates reference-type specialization template-specialization

12
推荐指数
1
解决办法
6577
查看次数

C++链接和模板特化

我正在研究C++链接器在模板特化方面的行为.我正在使用Microsoft Visual C++ 2010进行这些实验.我不知道其他工具链(例如gcc)的行为是否相同.

这是第一个代码片段:

// bar.cpp

template <typename T> int foo() { return 1; }
int bar() { return foo<double>(); }

// main.cpp

template <typename T> int foo() { return 1; }
template <> int foo<double>() { return 2; }

int bar();

int main()
{
    const int x = bar();
    const int y = foo<double>();  // doesn't link
}
Run Code Online (Sandbox Code Playgroud)

预计,此代码不会链接,因为它foo<double>()有多个定义,因为它在bar.cpp中实例化一次,在main.cpp中实例化一次(通过专门化).如果这个程序将链接,那么我们会期望bar()并且main()将使用不同的实例,foo()这样最终我们将得到x == 1和y == 2.

让我们通过声明 …

c++ linker templates inline specialization

12
推荐指数
1
解决办法
3745
查看次数

如何防止std :: vector <bool>的特化

我有一个模板类,它有一个类型的数据成员std::vector<T>,其中T也是我的模板类的参数.

在我的模板类中,我有一些逻辑可以做到这一点:

T &value = m_vector[index];
Run Code Online (Sandbox Code Playgroud)

当T是布尔值时,这似乎不会编译,因为std :: vector的[]运算符不返回bool-reference,而是返回不同的类型.

一些替代方案(虽然我不喜欢其中任何一个):

  • 告诉我的用户他们不能使用bool作为模板参数
  • 我的班级专门为bool(但这需要一些代码重复)

有没有办法告诉std :: vector不专门为bool?

c++ templates boolean vector specialization

12
推荐指数
1
解决办法
1617
查看次数

制作功能模板专业化虚拟法律?

在C++中,函数模板特化应该与正常函数完全相同.这是否意味着我可以制作一个虚拟的?

例如:

struct A
{
    template <class T> void f();
    template <> virtual void f<int>() {}
};

struct B : A
{
    template <class T> void f();
    template <> virtual void f<int>() {}
};

int main(int argc, char* argv[])
{
    B b;
    A& a = b;
    a.f<int>();
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2005给出了以下错误:

致命错误C1001:编译器中发生内部错误.

c++ virtual templates specialization c1001

11
推荐指数
1
解决办法
5367
查看次数

参考指针的函数模板特化

我有一个模板功能:

template<typename T>
void foo(const T& value) { bar(value); x = -1; }
Run Code Online (Sandbox Code Playgroud)

我想专门针对一组类型:

template<>
void foo<char>(const char& value) { bar(value); x = 0; }
template<>
void foo<unsigned char>(const unsigned char& value) { bar(value); x = 1; }
Run Code Online (Sandbox Code Playgroud)

它工作正常.当我编译这个:

template<>
void foo<char*>(const char*& value) { bar(value); x = 2; }
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

error C2912: explicit specialization; 'void foo(const char *&)' is not a specialization of a function template
Run Code Online (Sandbox Code Playgroud)

是否可以专门使用char*指针类型参数而不用typedef?

c++ templates specialization

11
推荐指数
1
解决办法
6806
查看次数

为什么在类中不允许使用函数模板特化?

在找到关于stackoverflow的许多问题的答案之后,我现在遇到了一个我无法找到答案的问题,我希望有人愿意帮助我!

我的问题是我想在C++中对一个类中的函数进行明确的模板化.我的编译器(g ++)和C++标准(§14.7.3)中的一个看起来告诉我,这个特化必须在声明类的命名空间中完成.我明白这意味着我不能把专业化放在课堂里,但是我没有看到这个限制的重点!有没有人知道是否有充分的理由不让专业在课堂上进行?

我知道有一些解决方法,例如将函数放在结构体中,但我想理解为什么语言有这种设计.如果有充分的理由不在课堂上允许专门的功能,我想在尝试解决它之前我应该​​知道它.

提前致谢!


为了让我的问题更加精确:以下是一些测试示例中的代码,说明了我想要做的事情:

#include <cstdio>

namespace MalinTester {

template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
    SpecializationTest() {
        privateVariable = 5;
    };
    virtual ~SpecializationTest() {};

    void execute() {
        execute<DIMENSIONALITY>();
    };

private:
    int privateVariable;
    template <size_t currentDim>
    static void execute() {
        printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
        execute<currentDim-1>();
    }

    template <>
    static void execute<0>() {
        printf("This is the base case. Current dim is 0.\n");
    }

};
Run Code Online (Sandbox Code Playgroud)

这是不可能的; g ++说: …

c++ templates explicit class specialization

11
推荐指数
1
解决办法
6940
查看次数

类范围中的类模板特化?

为什么A中的专业化S和B中的S不是?

(如果B未被注释掉)GCC 4.8.1:错误:非命名空间范围'class B'中的显式特化

#include <type_traits>
#include <iostream>

class Y {};
class X {};

struct A {
  template<class T, class = void>
  class S;

  template<class T>
  struct S < T, typename std::enable_if< std::is_same< Y, T >::value >::type > 
  {
    int i = 0;
  };

  template<class T>
  struct S < T, typename std::enable_if< std::is_same< X, T >::value >::type > 
  {
    int i = 1;
  };
};

/*
class B
{
    template<class T>
    class S;

    template<>
    class S < Y > {}; …
Run Code Online (Sandbox Code Playgroud)

c++ templates nested-class specialization

11
推荐指数
1
解决办法
9941
查看次数