此问题在此处已有答案:
Why is the const lost in an expression like const T&
where T is an rvalue reference?(1个答案)
2天前关闭。
当我运行下面的代码时,发生了一个错误。
#include <iostream>
using namespace std;
class M {
public:
M(): a(0), b(0) {}
M(int a, int b): a(a), b(b) {}
private:
int a;
int b;
};
template <typename T>
class Test {
public:
Test(const T& my) { }
T my_;
};
template <typename T>
using ReferTest = Test<T&>;
int main()
{
ReferTest<M> t2(M(3,4));
}
字符串
错误为:
main.cpp: In function ‘int main()’:
main.cpp:25:20: error: cannot bind non-const lvalue reference of type ‘M&’ to an rvalue of type ‘M’
25 | ReferTest<M> t2(M(3,4));
| ^~~~~~
main.cpp:16:17: note: initializing argument 1 of ‘Test<T>::Test(const T&) [with T = M&]’
16 | Test(const T& my) { }
| ~~~~~~~~~^~
型
“cannot bind non-const lvalue reference of type 'M&' to an rvalue of type 'M'”让我很困惑,我知道M(3,4)是一个右值,但是Test的构造函数中的类型是“const T&",而不是“non-const”。
1条答案
按热度按时间nhn9ugyo1#
我认为你应该在你的模板中使用一个转发引用,而不是像这样的一个常量,它会复制临时对象。像你这样使用临时对象的引用是很棘手的。
字符串