从声明中分离定义模板类的模板成员 [英] Defining a Template Member of a Template Class Seperately From The Declaration
问题描述
#include< cstdlib>
template< class A> struct Foo
{
template< class B> static bool Bar();
};
template< class B>模板<类A> bool Foo A :: Bar B()
{
return true;
}
int main()
{
bool b = Foo< int> :: Bar< long>();
b;
}
这会导致链接器错误:
main.obj:error LNK2019:未解析的外部符号public:static bool __cdecl Foo< int> :: Bar< long>(void) @J @?$ Foo @ H @@ SA_NXZ)在函数main中引用
成员函数在类模板的声明之外。换句话说,我不能这样做:
#include< cstdlib>
template< class A> struct Foo
{
template< class B> static bool Bar()
{
return true;
}
};
int main()
{
bool b = Foo< int> :: Bar< long>();
b;
}
我遗漏了什么?如何定义此成员函数模板?需要的语法是什么?
注意:我使用MSVC 2008,以防万一相关。
/ p>
我尝试的第一件事是颠倒 template< class A>
和 template< class B>
:
#include< cstdlib&
template< class A> struct Foo
{
template< class B>静态bool Bar();
};
template< class A>模板< class B> bool Foo A :: Bar B()
{
return true;
}
int main()
{
bool b = Foo< int> :: Bar< long>();
b;
}
这导致编译器错误:
.\main.cpp(11):error C2768:'Foo :: Bar':非法使用显式模板参数
在 Bar
函数的定义的结束括号。 p>
只要颠倒 template< class B>模板< class A>
。第二个是内部,并与成员声明一起。参见§14.5.2/ 1。
另外,如John指出,从 Bar< B> $ c>删除参数列表$ c>。
//outertemplate:此参数取代创建inner模板
template< ; A类>
//inner模板独立于外部替换之后
template< B类>
bool
//这只是outer替换后的类名。
foo< A>
//这有通常的函数模板语法
:: Bar(){
#include <cstdlib>
template<class A> struct Foo
{
template<class B> static bool Bar();
};
template<class B> template<class A> bool Foo<A>::Bar<B>()
{
return true;
}
int main()
{
bool b = Foo<int>::Bar<long>();
b;
}
This results in a linker error:
main.obj : error LNK2019: unresolved external symbol "public: static bool __cdecl Foo<int>::Bar<long>(void)" (??$Bar@J@?$Foo@H@@SA_NXZ) referenced in function main
I need to define this member function outside of the class template's declaration. In other words, I cannot do this:
#include <cstdlib>
template<class A> struct Foo
{
template<class B> static bool Bar()
{
return true;
}
};
int main()
{
bool b = Foo<int>::Bar<long>();
b;
}
What am I missing? How can I define this member function template? What's the needed syntax?
Note: I am using MSVC 2008, in case that's relevant.
EDIT
The sirst thing I tried was to reverse the order of template<class A>
and template<class B>
:
#include <cstdlib>
template<class A> struct Foo
{
template<class B> static bool Bar();
};
template<class A> template<class B> bool Foo<A>::Bar<B>()
{
return true;
}
int main()
{
bool b = Foo<int>::Bar<long>();
b;
}
This resulted in a compiler error:
.\main.cpp(11) : error C2768: 'Foo<A>::Bar' : illegal use of explicit template arguments
On the closing brace of the definition for the Bar
function.
Just reverse the order of template<class B> template<class A>
. The second one is "inner" and goes with the member declaration. See §14.5.2/1.
Also, as John points out, remove the parameter-list from Bar<B>
.
// "outer" template: this parameter gets substituted to create "inner" template
template< class A >
// "inner" template stands alone after "outer" substitution
template< class B >
bool
// This will just be a class name after "outer" substitution.
foo<A>
// This has usual function template syntax
:: Bar() {
这篇关于从声明中分离定义模板类的模板成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!