Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法专门化向量类和抽象类的模板函数_C++_Visual Studio 2010_Templates_Vector_Specialization - Fatal编程技术网

C++ 无法专门化向量类和抽象类的模板函数

C++ 无法专门化向量类和抽象类的模板函数,c++,visual-studio-2010,templates,vector,specialization,C++,Visual Studio 2010,Templates,Vector,Specialization,我正在尝试编写一个模板函数,但我无法同时为vector和其他类专门化它。以下是我正在使用的代码: // template definition template< class T > void f( const T& value) { cout << "DEFAULT" << endl; } // specialization for MyClass template< class T > void f<> ( c

我正在尝试编写一个模板函数,但我无法同时为vector和其他类专门化它。以下是我正在使用的代码:

// template definition
template< class T >
void f( const T& value) 
{
    cout << "DEFAULT" << endl;
}

// specialization for MyClass
template< class T >
void f<>  ( const MyClass & value )
{
    cout << "MyClass" << endl;
}

// specialization for vector
template< class T >
void f<>( const std::vector<T> & v )
{
    cout << "vector" << endl;
}
最后,
main
函数:

int main(int nArgs, char *vArgs[]){

    MyClass2 e;
    f<MyClass>(e);

}
int-main(int-nArgs,char*vArgs[]){
MyClass2 e;
f(e);
}
以下是我尝试使用Visual Studio 2010编译时遇到的错误:

c:\ProgramFiles(x86)\microsoft visual studio 10.0\vc\include\vector(869):错误C2259:“MyClass”:无法实例化抽象类

这似乎对这种特殊情况非常具体:只要我删除
常量
修饰符,我就将
向量
更改为
列表
,或者我将
MyClass
具体化,一切都会正常工作。但对于我的问题,我需要让这种特殊情况发挥作用


有没有人和我犯过同样的错误,更重要的是,有没有人知道如何解决这个问题?

您在专门化语法方面有错误

可能应该是这样的:

// MyClass
class MyClass{
virtual void a() = 0;
};

class MyClass2 : public MyClass{
    void a(){}
};

// template definition
template< class T >
void f(const T& value)
{
    cout << "DEFAULT" << endl;
};

// specialization for MyClass
template<>
void f(const MyClass & value)
{
    cout << "MyClass" << endl;
};

// specialization for vector of MyClass
template<>
void f(const std::vector<MyClass> & v)
{
    cout << "vector" << endl;
}

int main()
{
    // Using template for any type
    f(2);

    // Using specialization for MyClass
    MyClass2 e;
    f<MyClass>(e);

    // Using specialization for vector<MyClass>
    vector<MyClass> vM;
    f(vM);
}
//MyClass
类MyClass{
虚空a()=0;
};
类MyClass2:公共MyClass{
void a(){}
};
//模板定义
模板
空f(常数T和值)
{
我相信:

// specialization for vector
template< class T >
void f<>( const std::vector<T> & v )
{
    cout << "vector" << endl;
}
//向量的专门化
模板
空f(常数标准::向量和v)
{

cout如其他答案所示:

  • 您的专门化语法是错误的
  • 不能部分专门化模板函数
…有人知道这方面的解决方法吗

由于您使用的是参数列表中的模板参数,因此可以使用重载函数

template <class T>
void f(const T& value)
{
    std::cout << "DEFAULT" << std::endl;
}

void f(const MyClass& value)
{
    std::cout << "MyClass" << std::endl;
}

template <class T>
void f(const std::vector<T>& v) // this is an overload, not a specialization
{
    std::cout << "vector" << std::endl;
}

int main()
{
    f(1);
    MyClass2 e;
    f(e);
    f(std::vector<int>());
}
模板
空f(常数T和值)
{

std::cout
templatevoid f(const MyClass&value)
templatevoid f(const std::vector&v)
是不正确的代码行。你能再详细说明一下吗?这不是专门化的正确语法。你在哪里看到的?我不能编译你的代码,MyClass是抽象的,我不能在向量中实例化它。我将MyClass添加到示例中。现在应该编译它。你不需要在向量中实例化它,尽管这段代码在对于我来说,在GCC 4.7.2和VS 2013下,它不会在VS 2010下建立,所以OP最初的问题也同样如此。<代码> STD::vector < /代码>元素是一个抽象类,看起来好像是不正确的代码……我想你找到了根本原因:“C++标准下不允许部分函数特化”。.我仍然想知道为什么当我将向量更改为专用函数参数中的列表时,我的代码编译得很好,并且我能够调用
f(e)
。我想你会说这是“偶然的”?@ArthurBall我真的一点也不确定为什么
列表
专业化有效,而
向量
一个不起作用。我很想把它归结为VS 2010的一些特殊怪癖;它有很多怪癖(毕竟,代码描述的是非标准行为)。我无法在VS 2010下准确地实例化
列表
,但是…这看起来确实像是一个特定的编译器或库问题,因为其他编译器可以管理该位。你是对的,我函数的
向量
版本不是一个专门化,而是一个重载。尽管如此,我发现部分专门化工作正常很奇怪对于类,而不是函数。@ArthurBall我认为它不能很好地解决重载问题。此外,函数和类是不同的东西,所以模板函数和模板类也是不同的。也不可能推断类的模板参数,例如在调用构造函数时。我更怀念这一点模板函数的优化。
template <class T>
void f(const T& value)
{
    std::cout << "DEFAULT" << std::endl;
}

void f(const MyClass& value)
{
    std::cout << "MyClass" << std::endl;
}

template <class T>
void f(const std::vector<T>& v) // this is an overload, not a specialization
{
    std::cout << "vector" << std::endl;
}

int main()
{
    f(1);
    MyClass2 e;
    f(e);
    f(std::vector<int>());
}