Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++;功能模板功能等效?_C++_C++11_Language Lawyer - Fatal编程技术网

C++ 这两个看起来是相同的C++;功能模板功能等效?

C++ 这两个看起来是相同的C++;功能模板功能等效?,c++,c++11,language-lawyer,C++,C++11,Language Lawyer,假设我们有一个程序,它定义了几个函数模板: template<typename T> void g(T) {} struct A {}; //this template only sees the definition of g above //the usage of g does not depend on the parameter of the template template<typename T = void> decltype(T(), (g)(A{}

假设我们有一个程序,它定义了几个函数模板:

template<typename T>
void g(T) {}

struct A {};

//this template only sees the definition of g above
//the usage of g does not depend on the parameter of the template
template<typename T = void>
decltype(T(), (g)(A{})) f(A a)
{
   return (g)(a);
}

A g (A a)
{
  return a;
}
//this template sees both overloads of g
//and still the usage of g does not depend on the parameter of the template
template<typename T = void>
decltype(T(), (g)(A{})) f(A a)
{
  return (g)(a);
}
模板
空位g(T){}
结构A{};
//此模板仅查看上面g的定义
//g的使用不依赖于模板的参数
模板
decltype(T(),(g)(A{}))f(A)
{
申报表(g)(a);
}
A g(A A)
{
返回a;
}
//此模板可以看到g的两个重载
//而且g的用法并不依赖于模板的参数
模板
decltype(T(),(g)(A{}))f(A)
{
申报表(g)(a);
}
此程序包含定义函数模板f的代码的两个副本。一般来说,C++标准禁止创建等效重载。 问题是,根据标准,这两个模板是否等效

如果这两个模板使用g作为依赖名称,它们将被视为等价的,正如标准明确规定的那样:

为了确定两个依赖名称([temp.dep])是否相等,只考虑名称本身,而不考虑模板上下文中名称查找的结果。如果同一函数模板的多个声明在此名称查找的结果中不同,则使用第一个声明的结果

但是在这里,g的用法并不依赖于T(而作为依赖于T的表达式的一部分)


编译器不同意这两个定义是否允许:CLAN接受它,GCC和VisualC++没有(参见)。< /P>你想基于返回类型过载吗?@ ToBi303模板可以基于返回类型重载,但它只是一个虚构的例子。我错过了语言律师标签并想知道这将有什么好处;你打算允许一个T重载,可能会改变答案吗?@Yakk,我不想。假设我有decltype((void)T(),(g)(A{}))。