Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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++ - Fatal编程技术网

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()
{ 
} 

您看到的正是这些代码吗?它是否在
对象
之间缺少某些内容?它是否等效?难道你不需要在
对象
对象之间至少写
??