Re: [翻譯] Rvalue References: C++0x Features in …
回一下這篇翻譯的文章~
不知道大家有沒有試過這個範例~ 在講
move semantics: moving from lvalues
要把 lvalue 轉成 rvalue
會有編譯錯誤~ 自己試試看改了很久 還是改不出來
所以問一下有認真看這篇文章的板友
我是用 VC 2010 Express 4.0.30319 RTMRel
~
錯誤訊息是
error C2440: 'return' : cannot convert from 'remote_integer' to
'remote_integer &&'
You cannot bind an lvalue to an rvalue reference
Test2010.cpp(49) : see reference to function template instantiation
'remote_integer &&nMove<remote_integer&>(T)' being compiled
with
[
T=remote_integer &
]
既然 T 已經是 remote_integer & 型別
但是會有 cannot convert from 'remote_integer' to 'remote_integer &&'
的錯誤 真的頭大~
: #include <stddef.h>
: #include <iostream>
: #include <ostream>
: using namespace std;
: template <typename T> struct RemoveReference {
: typedef T type;
: };
: template <typename T> struct RemoveReference<T&> {
: typedef T type;
: };
: template <typename T> struct RemoveReference<T&&> {
: typedef T type;
: };
: template <typename T> typename RemoveReference<T>::type&& Move(T&& t) {
: return t;
: }
: class remote_integer {
: public:
: remote_integer() {
: cout << "Default constructor." << endl;
: m_p = NULL;
: }
: explicit remote_integer(const int n) {
: cout << "Unary constructor." << endl;
: m_p = new int(n);
: }
: remote_integer(const remote_integer& other) {
: cout << "Copy constructor." << endl;
: m_p = NULL;
: *this = other;
: }
: #ifdef MOVABLE
: remote_integer(remote_integer&& other) {
: cout << "MOVE CONSTRUCTOR." << endl;
: m_p = NULL;
: *this = Move(other); // RIGHT
: }
: #endif // #ifdef MOVABLE
: remote_integer& operator=(const remote_integer& other) {
: cout << "Copy assignment operator." << endl;
: if (this != &other) {
: delete m_p;
: if (other.m_p) {
: m_p = new int(*other.m_p);
: } else {
: m_p = NULL;
: }
: }
: return *this;
: }
: #ifdef MOVABLE
: remote_integer& operator=(remote_integer&& other) {
: cout << "MOVE ASSIGNMENT OPERATOR." << endl;
: if (this != &other) {
: delete m_p;
: m_p = other.m_p;
: other.m_p = NULL;
: }
: return *this;
: }
: #endif // #ifdef MOVABLE
: ~remote_integer() {
: cout << "Destructor." << endl;
: delete m_p;
: }
: int get() const {
: return m_p ? *m_p : 0;
: }
: private:
: int * m_p;
: };
: remote_integer frumple(const int n) {
: if (n == 1729) {
: return remote_integer(1729);
: }
: remote_integer ret(n * n);
: return ret;
: }
: int main() {
: remote_integer x = frumple(5);
: cout << x.get() << endl;
: remote_integer y = frumple(1729);
: cout << y.get() << endl;
: }
: C:\Temp>cl /EHsc /nologo /W4 /O2 /DMOVABLE unified_right.cpp
: unified_right.cpp
: C:\Temp>unified_right
: Unary constructor.
: MOVE CONSTRUCTOR.
: MOVE ASSIGNMENT OPERATOR.
: Destructor.
: 25
: Unary constructor.
: 1729
: Destructor.
: Destructor.
Test2010.cpp(19) : error C2440: 'return' : cannot convert from
'remote_integer' to 'remote_integer &&'
You cannot bind an lvalue to an rvalue reference
Test2010.cpp(49) : see reference to function template instantiation
'remote_integer &&nMove<remote_integer&>(T)' being compiled
with
[
T=remote_integer &
]
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.189.231
※ 編輯: fuha 來自: 59.120.189.231 (05/03 17:35)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章