gpt4 book ai didi

c++ - 为什么在从通用引用推导类型时忽略 const?

转载 作者:太空狗 更新时间:2023-10-29 22:58:27 26 4
gpt4 key购买 nike

这是我的 A 类的定义并为其创建函数:

template< typename T >
class A
{
public:
A( T test )
: _test( test )
{}

public:
const T _test;
};

template <typename T>
A<T> make_a (T&& elem) {
return A<T>{std::forward<T>(elem)};
}

当我将 int 左值传递给 make_a 函数时,我希望实例化的类类似于

class B
{
public:
B( int &test )
: _test( test )
{}

public:
const int &_test;
};

但是 A::_test 的类型被推断为 int& 而不是 const int&:

int main(int argc, const char * argv[]) {

int a = 1;
auto aa = make_a(a);
aa._test = 2; // compiled OK

B bb(a);
bb._test = 2; // compilation error
return 0;
}

谁能解释一下是什么导致了这种行为?

我正在使用 XCode 7.0、LLVM 7.0 默认编译器。

谢谢。

最佳答案

此行为是由语言标准引起的。

N4140 §8.3.2 [dcl.ref]/1

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef-name (7.1.3, 14.1) or decltype-specifier (7.1.6.2), in which case the cv-qualifiers are ignored. [ Example:

typedef int& A;
const A aref = 3; // ill-formed; lvalue reference to non-const initialized with rvalue

The type of aref is “lvalue reference to int”, not “lvalue reference to const int”. —end example ]

关于c++ - 为什么在从通用引用推导类型时忽略 const?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40568170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com