相关疑难解决方法(0)

使用 decltype(auto) 推导出的返回类型是否正确?

decltype(auto) func(int&& n)
{
    return (n);
}
Run Code Online (Sandbox Code Playgroud)

clang 12.0.1接受此代码,但gcc 11.2.1拒绝它:

error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'int' 
Run Code Online (Sandbox Code Playgroud)

gcc似乎使用 return type int &&,但带有n内括号,这是正确的吗?

神电演示

c++ language-lawyer decltype-auto

13
推荐指数
1
解决办法
297
查看次数

GCC的decltype(auto)不符合标准?

我尝试使用不同的选项在GCC 8.2下编译这个C++代码,它总是成功,不产生警告和输出true:

int && a = 123;
decltype(auto) b = a;

std::cout << std::boolalpha << std::is_same<decltype(b), int&>::value;
Run Code Online (Sandbox Code Playgroud)

同时,相同的代码不会在Clang中编译,如果我对标准的理解是正确的,那就是符合标准的行为.

cppreference decltype:

如果参数是未加密码的id-expression或未加括号的类成员访问表达式,则decltype将生成此表达式命名的实体的类型.

cppreference decltype(auto):

如果声明的变量类型是decltype(auto),则将关键字auto替换为其初始化程序的表达式(或表达式列表),并使用decltype规则推导出实际类型.

因此,decltype(auto)应该屈服int&&.因为它a是一个左值,它不应该绑定b,导致编译错误.

那么海湾合作委员会是否不符合标准或者是否有我遗漏的东西?

c++ gcc clang decltype auto

8
推荐指数
1
解决办法
200
查看次数

标签 统计

c++ ×2

auto ×1

clang ×1

decltype ×1

decltype-auto ×1

gcc ×1

language-lawyer ×1