具有多个模板参数错误的模板部分专门化 [英] Template partial specialization with multiple template argument error
问题描述
当我使用一个模板参数的类的模板部分专门化时,我可以专门化这样的方法:
include< cstdlib>
模板< std :: size_t Dim>
class Test
{
public:
int foo();
};
模板< std :: size_t Dim>
inline int Test< Dim> :: foo()
{
return 0;
}
模板<>
inline int Test< 1> :: foo()
{
return 1;
}
int main()
{
Test< 2。 wTest2;
Test< 1。 wTest1;
wTest2.foo();
wTest1.foo();
return 0;
}
方法foo专用于Dim = 1。我的类的模板参数,像这样:
#include< cstdlib>
模板< typename T,std :: size_t Dim>
class Test
{
public:
int foo();
};
模板< typename T,std :: size_t Dim>
inline int Test< T,Dim> :: foo()
{
return 0;
}
template<类型名T>
inline int Test< T,1> :: foo()
{
return 1;
}
int main()
{
Test<双,2> wTest2;
Test<双,1> wTest1;
wTest2.foo();
wTest1.foo();
return 0;
}
VS2010的编译器抱怨出现这些错误:
1> c:\documents和settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20):错误C3860:模板参数列表下面的类模板名称必须按照模板参数列表中使用的顺序列出参数
1> c:\documents和settings\cayouette\my documents\codelocal\testtemplatespecialization\main .cpp(20):error C2995:'int Test< T,Dim> :: foo(void)':function template has been defined
1& c:\documents和settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(7):参见'Test< T,Dim> :: foo'的声明
1> c :\documents和settings \cayouette\my documents \ codelocal\testtemplatespecialization\main.cpp(20):error C2976:'Test< T,Dim>':模板参数太少
1& c:\documents和settings \cayouette\my documents \codelocal\testtemplatespecialization\main.cpp(26):错误C2264:'Test< T,Dim> :: foo':函数定义或声明中的错误;函数不调用
1> with
1> [
1> T = double,
1> Dim = 2
1> ]
1> c:\documents和settings \cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(27):error C2264:'Test< T,Dim> :: foo' :函数定义或声明中的错误;函数不调用
1> with
1> [
1> T = double,
1> Dim = 1
1> ]
1>
1>构建失败。
我看到这种方式,没有歧义,编译器应该能够解决一切
如果在C ++中不支持,请解释为什么。
您不能部分地专门化功能 - 包括成员函数。
模板< typename T,std :: size_t Dim>
class Test
{
public:
int foo()
{
return 0;
}
};
模板<类型名T>
class test< T,1>
{
public:
int foo()
{
return 1;
}
};
(我在这里定义了函数inline;当然不是必须的) p>
When I use template partial specialization on a class with one template argument, I can specialize a method like this:
#include <cstdlib>
template< std::size_t Dim >
class Test
{
public:
int foo();
};
template< std::size_t Dim >
inline int Test< Dim >::foo()
{
return 0;
}
template<>
inline int Test< 1 >::foo()
{
return 1;
}
int main()
{
Test< 2 > wTest2;
Test< 1 > wTest1;
wTest2.foo();
wTest1.foo();
return 0;
}
The method foo is specialized for Dim = 1. But as soon as I add a template argument to my class, like this:
#include <cstdlib>
template< typename T, std::size_t Dim >
class Test
{
public:
int foo();
};
template< typename T, std::size_t Dim >
inline int Test< T, Dim >::foo()
{
return 0;
}
template< typename T >
inline int Test< T, 1 >::foo()
{
return 1;
}
int main()
{
Test< double, 2 > wTest2;
Test< double, 1 > wTest1;
wTest2.foo();
wTest1.foo();
return 0;
}
The compiler (of VS2010) complains with these errors:
1>c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20): error C3860: template argument list following class template name must list parameters in the order used in template parameter list
1>c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20): error C2995: 'int Test<T,Dim>::foo(void)' : function template has already been defined
1> c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(7) : see declaration of 'Test<T,Dim>::foo'
1>c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20): error C2976: 'Test<T,Dim>' : too few template arguments
1>c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(26): error C2264: 'Test<T,Dim>::foo' : error in function definition or declaration; function not called
1> with
1> [
1> T=double,
1> Dim=2
1> ]
1>c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(27): error C2264: 'Test<T,Dim>::foo' : error in function definition or declaration; function not called
1> with
1> [
1> T=double,
1> Dim=1
1> ]
1>
1>Build FAILED.
The way I see this, there is no ambiguity and the compiler should be able to resolve everything and work just like the one argument case.
If this is not supported in C++, please explain why.
You cannot partially specialise functions – this includes member functions. You can only partially specialise the whole class:
template< typename T, std::size_t Dim >
class Test
{
public:
int foo()
{
return 0;
}
};
template< typename T >
class test< T, 1 >
{
public:
int foo()
{
return 1;
}
};
(I’ve defined the functions inline here; that of course isn’t necessary.)
这篇关于具有多个模板参数错误的模板部分专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!