类成员变量的Decltype是否被禁止?为什么

decltype of a class member variable, is it forbidden? Why?

本文关键字:禁止 为什么 是否 Decltype 成员 变量      更新时间:2023-10-16

我可以写

int a;
decltype(a) b;

但是我的编译器(Microsoft Visual Studio 2010, cl Version 16.00.40219.01)禁止我

class A
{
   int a;
   decltype(a) b;// error C2327: 'A::a' : is not a type name, static, or enumerator
};

也comau C/c++ 4.3.10.1 (Oct 6 2008 11:28:09)对于ONLINE_EVALUATION_BETA2给出了类似的错误。

"ComeauTest.c", line 7: error: a nonstatic member reference must be relative to a
          specific object
     decltype(a) b;
              ^

这种行为的基本原理是什么?

是有效的。在未求值的操作数(decltype, sizeof, noexcept,…的操作数)中,也可以在成员函数之外命名非静态数据成员。

你尝试过的编译器还不支持这个特性