类本地作为C++ 11之前的谓词

Rik*_*der 4 c++ compiler-errors predicate language-lawyer c++11

使用GCC和Clang在使用C++ 11模式构建时,以下代码编译时没有错误/警告.但是,如果我尝试在没有C++ 11模式的情况下编译,并且在第二个范围内发生错误.

#include <algorithm>
#include <vector>

struct astruct
{
   int v;
};

struct astruct_cmp0
{
   bool operator()(const astruct& a0, const astruct& a1) {
     return a0.v < a1.v;
   }
};

int main()
{
   std::vector<astruct> alist;
   {
      // Works - no errors
      std::stable_sort(alist.begin(),alist.end(),astruct_cmp0());
   }

   {
      struct astruct_cmp1
      {
         bool operator()(const astruct& a0, const astruct& a1) {
           return a0.v < a1.v;
         }
      };

      // error: template argument uses local type 'astruct_cmp1'
      std::stable_sort(alist.begin(),alist.end(),astruct_cmp1());
   }

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:允许本地结构定义的C++ 11更改是什么?有人可以请我指出标准中的具体部分(也许是第9.8节)

Die*_*ühl 6

在C++ 03函数中,本地类型不是可行的模板参数.在C++ 11函数中,本地类型是可行的模板参数.C++ 03中的关键引用是14.3.1 [temp.arg.type]第2段:

以下类型不能用作模板类型参数的模板参数:

  • 名称没有链接的类型
  • ...

在C++ 11中,删除了此约束.

关于何时定义链接的相关部分是3.5 [basic.link](在两个标准中),它相当长并指向没有排除链接的实体,C++ 03中的第8段:

这些规则未涵盖的名称没有链接....

函数中定义的类型未在"这些规则"中列出.