decltype(auto) 变量是否允许使用 cv 限定符?

Are cv-qualifiers allowed on decltype(auto) variables?

标准规定

If the placeholder is the decltype(auto) type-specifier, T shall be the placeholder alone.

decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)

尚不清楚是否仍允许使用 cv 限定符。如果允许的话,这是有道理的。编译器似乎不同意这个问题。下面的代码 is accepted by g++ but rejected by clang++, vc++ 似乎根本不支持 decltype(auto) 变量:

int main()
{
    const decltype(auto) sz_text{"test"};
}

要回答这个问题,我们需要引用上一段,它指定了 T 是什么。在这种情况下,[dcl.type.auto.deduct]/2 表示(强调我的):

A type T containing a placeholder type, and a corresponding initializer e, are determined as follows:

  • for a variable declared with a type that contains a placeholder type, T is the declared type of the variable and e is the initializer. If the initialization is direct-list-initialization, the initializer shall be a braced-init-list containing only a single assignment-expression and e is the assignment-expression;

在这种情况下,T 整个 声明的类型 sz_text、cv 限定符和所有。你引用的段落很清楚,如果它包含 decltype(auto) 作为占位符,那一定是那个,仅此而已。

所以是一个 GCC 错误。 And an already reported one.