C++ 双模板<&燃气轮机;声明
有这样的函数定义:C++ 双模板<&燃气轮机;声明,c++,C++,有这样的函数定义: template<> template<> void object::test<1>() { } template <int m=1> class object { public: template <int n> void test(); }; // This differs from your example, by the addition of `<>` after `object`,
template<>
template<>
void object::test<1>()
{
}
template <int m=1>
class object
{
public:
template <int n>
void test();
};
// This differs from your example, by the addition of `<>` after `object`, but it's as
// close as I can come to valid code true to your example
template<>
template<>
void object<>::test<1>()
{
}
模板
模板
void对象::test()
{
}
有双模板是什么意思
编辑:
我提取了对本例有效的代码:
#include <iostream>
template <class U>
class A {
template <int n>
void test() {
}
};
template <class T>
class B {
public:
typedef A<T> object;
};
typedef B<int>::object object;
template<>
template<>
void object::test < 1 > () {
}
int main() {
return 0;
}
#包括
模板
甲级{
模板
无效测试(){
}
};
模板
B类{
公众:
键入一个对象;
};
typedefb::对象;
模板
模板
无效对象::测试<1>(){
}
int main(){
返回0;
}
这段代码在g++下编译
来源:例如
template<class T = int> // Default T is int
class object<T> {
template<int R> void test ();
};
template//默认T为int
类对象{
模板无效测试();
};
您的代码:
template<> // T is fixed
template<> // R is fixed
void object<>::test<1>() // T is default int, R is 1
{
}
template//T是固定的
模板//R是固定的
void object::test()//T为默认int,R为1
{
}
相当于:
template<> // T is fixed
template<> // R is fixed
void object<int>::test<1>() // T is int, R is 1
{
}
template//T是固定的
模板//R是固定的
void object::test()//T是int,R是1
{
}
这是类模板成员函数模板的模板专门化(我做对了吗?),带有模板的默认参数。看看这个:
template<typename T = int>
struct X {
template<typename U>
void foo(U u);
};
template<>
template<>
void X::foo(float f) { }
模板
结构X{
模板
void foo(U);
};
模板
模板
void X::foo(float f){}
这为以下情况引入了专门化:X
的模板参数是int
,而X::foo
的参数是float
。这种情况与您的情况略有不同,我们不必在成员函数的名称后显式提供模板参数,因为可以推断出模板参数。您的函数有一个非类型模板参数,该参数无法推导,因此必须提供
最让我困惑的是默认模板参数,我不确定使用skip是否是一种好的做法。我会为每个专门化提供它,以避免混淆。在我看来,这就像是类模板中函数模板的专门化。例如,考虑下面的类模板定义:
template<>
template<>
void object::test<1>()
{
}
template <int m=1>
class object
{
public:
template <int n>
void test();
};
// This differs from your example, by the addition of `<>` after `object`, but it's as
// close as I can come to valid code true to your example
template<>
template<>
void object<>::test<1>()
{
}
模板
类对象
{
公众:
模板
无效试验();
};
//这与您的示例不同,在“object”之后添加了“object”,但它是
//尽可能接近我可以得到的有效代码真实的例子
模板
模板
void对象::test()
{
}
您看到的正是这些代码吗?它是否在对象
和:
之间缺少某些内容?它是否等效?难道你不需要在对象
和对象之间至少写
:
??