Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.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++ 模板参数模板化的模板化类专门化:VisualStudio与g++;_C++_C++11_Templates_Gcc_Visual Studio 2015 - Fatal编程技术网

C++ 模板参数模板化的模板化类专门化:VisualStudio与g++;

C++ 模板参数模板化的模板化类专门化:VisualStudio与g++;,c++,c++11,templates,gcc,visual-studio-2015,C++,C++11,Templates,Gcc,Visual Studio 2015,下面的代码是我从阅读中获得的,在gcc()中编译并表现良好,但在visualstudio中给出了一个错误 错误C2910'my_属性。解决方案版本在g++中也可以 #include <iostream> #include <type_traits> template <typename T> struct A { T x; }; template <typename T> struct my_property { static cons

下面的代码是我从阅读中获得的,在gcc()中编译并表现良好,但在visualstudio中给出了一个错误

错误C2910'
my_属性。解决方案版本在g++中也可以

#include <iostream>
#include <type_traits>

template <typename T>
struct A {
  T x;
};

template <typename T>
struct my_property {
  static const bool value = false;
};

template <> //Remove this and it will work in Visual Studio
template <typename U>
struct my_property<A<U>> {
  static const bool value = true;
};

int main() 
{
  std::cout << std::boolalpha;
  std::cout << my_property<int>::value << '\n'; //false
  std::cout << my_property<A<int>>::value << '\n'; //true
  std::cout << my_property<A<float>>::value << '\n'; //true
}
#包括
#包括
模板
结构A{
tx;
};
模板
struct my_属性{
静态常量布尔值=假;
};
模板//删除此模板,它将在Visual Studio中工作
模板
struct my_属性{
静态常量布尔值=真;
};
int main()
{

std::cout如果我正确阅读了标准,则在这种情况下不应使用
模板

基本上,您必须为嵌套模板提供多个模板参数列表:

(见[临时成员]§1)

模板可以在类或类模板中声明;此类模板称为成员模板。成员模板可以在其类定义或类模板定义内部或外部定义。在其类模板定义外部定义的类模板的成员模板应使用模板参数指定类模板的参数后跟成员模板的模板参数。

…但是您不需要提供额外的模板参数列表,以便使用模板参数专门化模板:

(见[温度等级规范]§2)

每个类模板部分专门化都是一个不同的模板

(然后是§4)

模板参数在紧跟在关键字模板之后的尖括号内的列表中指定。对于部分专门化,模板参数列表直接写在类模板名称之后。对于主模板,此列表由模板参数列表隐式描述


没有任何迹象表明需要额外的模板参数列表-专门化只是一个模板,因此它只需要一个参数列表。

template
--这看起来像是胡说八道。@Yakk好吧,如果你仔细想想,这就是引入显式专门化的方式。它实际上看起来很漂亮ty对我来说很自然。也可以使用clang++编译,但带有“警告:模板专用化中的无关模板参数列表”(指向行
模板
)顺便说一句,Visual Studio
给出的错误不能明确地专门化
对我来说完全是误导,因为这实际上就是我们在这里能够实现的。这只是部分专门化。我只见过两行
模板
在引用另一个模板中的模板时使用ch不是这种情况。我认为这里的关键是,即使只有一个模板参数(如果您将一个模板参数视为可以“拆分”为TemplateTable类的内容),也可以进行部分专门化。部分专门化可以比部分专门化的类具有更多的模板参数!!