• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            羅朝輝(飄飄白云)

            關注嵌入式操作系統,移動平臺,圖形開發。-->加微博 ^_^

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              85 隨筆 :: 0 文章 :: 169 評論 :: 0 Trackbacks

            【譯】VC10中的C++0x特性 part 2(2):右值引用

            來源:vcblog 作者:Stephan T. Lavavej 翻譯:飄飄白云  

             

            (轉載時請注明作者和出處。未經許可,請勿用于商業用途)

            簡介

            這一系列文章介紹Microsoft Visual Studio 2010 中支持的C++ 0x特性,目前有三部分。
            Part 1 :介紹了Lambdas, 賦予新意義的auto,以及 static_assert;
            Part 2( , , ):介紹了右值引用(Rvalue References);
            Part 3:介紹了表達式類型(decltype)

            VC10中的C++0x特性 Part 1,2,3 譯文打包下載(doc 和 pdf 格式): 點此下載


            本文為 Part 2 第二頁。


            move 語意:從 lvalue 移動


            現在,如果你喜歡用拷貝賦值函數來實現你的拷貝構造函數該怎樣做呢,那你也可能試圖用 move 拷貝賦值函數來實現 move 構造函數。這樣作是可以的,但是你得小心。下面就是一個錯誤的實現:

             

            C:\Temp>type unified_wrong.cpp

            #include <stddef.h>

            #include <iostream>

            #include <ostream>

            using namespace std;

             

            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 = other; // WRONG

                }

            #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 unified_wrong.cpp

            unified_wrong.cpp

             

            C:\Temp>unified_wrong

            Unary constructor.

            Copy constructor.

            Copy assignment operator.

            Destructor.

            25

            Unary constructor.

            1729

            Destructor.

            Destructor.

             

            C:\Temp>cl /EHsc /nologo /W4 /O2 /DMOVABLE unified_wrong.cpp

            unified_wrong.cpp

             

            C:\Temp>unified_wrong

            Unary constructor.

            MOVE CONSTRUCTOR.

            Copy assignment operator.

            Destructor.

            25

            Unary constructor.

            1729

            Destructor.

            Destructor.

             

            (編譯器在這里進行了返回值優化(RVO),但不是具名返回值優化(NRVO)。就像我之前提到的,有些拷貝構造函數被 RVO 或 NRVO 優化掉了,但編譯器并不總是能夠做這樣的優化,這時剩余的就由 move 構造函數來優化。)

             

            move 構造函數中標記為 WRONG 的那一行,調用了拷貝賦值函數,編譯能通過也能運行,但這違背了 move 構造函數的本意。(譯注:因為那個拷貝賦值函數只是進行普通的拷貝賦值,而不是 move 賦值!)

             

            這是怎么回事呢?記?。涸贑++98/03中,具名 lvalue 引用是左值(給定語句 int& r = *p; r 是 lvalue),不具名 lvalue 引用還是左值(給定語句 vector<int> v(10, 1729), v[0] 返回 int&, 你可以對這個不具名 lvalue 引用取址)。但是 rvalue 引用就不一樣了:

             

            ? 具名 lvalue 引用是 lvalue

            ? 不具名 rvalue 引用是 rvalue。

             

            一個具名 rvalue 引用是一個 lvalue 是因為可以對它施加多重操作,重復使用。相反,如果它是一個 ravlue 的話,那么對它施加的第一個操作能夠“竊取”它,而后續操作就沒機會了。這里的“竊取”是說不會被察覺到,所以這是行不通的。另一方面,不具名 rvalue 引用不能被重復使用,所以它仍保持右值(rvalueness)語意。

             

            如果你真的打算用 move 賦值函數來實現 move 構造函數,你需要從 lvalue move,就像是從 rvalue move 一樣。C++0x <utility> 中的 std::move() 具備這樣的能力,VC10將會有這個(實際上,開發版中已經有了),但VC10 TCP版還沒有,所以我會教你從頭做起:

             

            C:\Temp>type unified_right.cpp

            #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.

             

            (我將交替使用 std::move() 和我自己的 Move(),因為它們的實現是等價的) std::move() 是怎樣工作的呢?目前,我只能跟你說這是“魔法”。(后面會有完整的解釋,并不復雜,但它與模板參數推導和引用折疊(reference collapsing,譯注:引用的引用)有 關,后面講完美轉發的時候我們還會遇到這兩個東西)。我可以用一個具體的例子來略過“魔法”:給定一個 string 類型的左值,像前面重載決議例子中的 up ,std::move(up) 調用 string&& std::move(string&),這個函數返回一個不具名的 rvalue 引用,它是一個 rvalue。給定一個 string 類型的 rvalue,像前面重載決議例子中的 strange(), std::move(strange()) 調用 string&& std::move(string&&),同樣這個函數還是返回一個不具名的 rvalue,還是 rvalue。

             

            std::move() 除了讓你能用 move 復制函數來實現 move 構造函數之外,還能在其他地方發揮作用。無論何時,只要你有一個左值,而它的值也不再重要了(例如,它將被銷毀或被賦值),你就可以使用 std::move(你的左值表達式) 來使用 move 語意。

             

            move 語意:可移動成員(movable member)


            C++0x 的標準類型(像 vector, string, regex) 都有 move 構造函數和 move 賦值函數。而且我們也已經看到了如何在我們自己的類中通過手動管理資源來實現 move 語意(像前面的 remote_integer 類)。如果類中包含可移動數據成員(像 vector, string, regex, remote_integer )時該怎么辦呢?編譯器不會自動幫我們自動產生 move 構造函數和 move 賦值函數,所以我們必須手動編寫它們。很幸運,有了 std::move() 編寫它們是很容易的。

             

            C:\Temp>type point.cpp

            #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;

             

                    if (other.m_p) {

                        m_p = new int(*other.m_p);

                    } else {

                        m_p = NULL;

                    }

                }

             

                remote_integer(remote_integer&& other) {

                    cout << "MOVE CONSTRUCTOR." << endl;

             

                    m_p = other.m_p;

                    other.m_p = NULL;

                }

             

                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;

                }

             

                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;

                }

             

                ~remote_integer() {

                    cout << "Destructor." << endl;

             

                    delete m_p;

                }

             

                int get() const {

                    return m_p ? *m_p : 0;

                }

             

            private:

                int * m_p;

            };

             

            class remote_point {

            public:

                remote_point(const int x_arg, const int y_arg)

                    : m_x(x_arg), m_y(y_arg) { }

             

                remote_point(remote_point&& other)

                    : m_x(Move(other.m_x)),

                      m_y(Move(other.m_y)) { }

             

                remote_point& operator=(remote_point&& other) {

                    m_x = Move(other.m_x);

                    m_y = Move(other.m_y);

                    return *this;

                }

             

                int x() const { return m_x.get(); }

                int y() const { return m_y.get(); }

             

            private:

                remote_integer m_x;

                remote_integer m_y;

            };

             

            remote_point five_by_five() {

                return remote_point(5, 5);

            }

             

            remote_point taxicab(const int n) {

                if (n == 0) {

                    return remote_point(1, 1728);

                }

             

                remote_point ret(729, 1000);

             

                return ret;

            }

             

            int main() {

                remote_point p = taxicab(43112609);

             

                cout << "(" << p.x() << ", " << p.y() << ")" << endl;

             

                p = five_by_five();

             

                cout << "(" << p.x() << ", " << p.y() << ")" << endl;

            }

             

            C:\Temp>cl /EHsc /nologo /W4 /O2 point.cpp

            point.cpp

             

            C:\Temp>point

            Unary constructor.

            Unary constructor.

            MOVE CONSTRUCTOR.

            MOVE CONSTRUCTOR.

            Destructor.

            Destructor.

            (729, 1000)

            Unary constructor.

            Unary constructor.

            MOVE ASSIGNMENT OPERATOR.

            MOVE ASSIGNMENT OPERATOR.

            Destructor.

            Destructor.

            (5, 5)

            Destructor.

            Destructor.

             

            現在你看到啦,按成員移動(memberwise move)是很容易做到的。注意, remote_point 的 move 賦值函數沒有進行自我賦值檢查,是因為 remote_integer 已經檢查過了。也要注意到 remote_point 隱式聲明的拷貝構造函數,拷貝賦值函數和析構函數都正常運作。

             

            到現在,你應該對 move 語意已經非常熟悉了。(希望不是抓狂?。。榱藴y試你新獲得的這個不可思議的技能,請為前面的例子寫一個 +() 操作符函數當作練習吧。

             

            最后的提醒:只要你的類支持 move 語意,你就應該實現 move 構造函數和 move 賦值函數。因為不僅僅是你平常使用這些類時可從 move 語意中獲利, STL 容器和算法也能從中獲利,通過廉價的 move 省下昂貴的拷貝開銷。


            (轉載請注明出處,作者與譯者信息,請勿用于商業用途) 


             < 前一頁  后一頁 >


            posted on 2009-06-04 09:37 羅朝輝 閱讀(2260) 評論(3)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: VC10中的C++0x特性 part 2(2):右值引用[未登錄] 2009-06-04 11:27 Terry
            翻譯得非常好!頂!  回復  更多評論
              

            # re: VC10中的C++0x特性 part 2(2):右值引用 2009-06-05 08:20 hlysh
            沒用,現在哪里開發用c++0x了?  回復  更多評論
              

            # re: 【譯】VC10中的C++0x特性 part 2(2):右值引用 2009-07-23 11:28 gaojl0728
            great work!  回復  更多評論
              

            国产呻吟久久久久久久92| 久久99久久99精品免视看动漫| 韩国免费A级毛片久久| 99久久久国产精品免费无卡顿| 久久国产精品99久久久久久老狼 | 欧美久久综合性欧美| 久久精品视频91| www.久久热| 久久精品一本到99热免费| 久久久久久久亚洲Av无码| 婷婷久久综合九色综合绿巨人| 久久青青草原亚洲av无码app| 久久这里只有精品首页| 无码人妻久久一区二区三区免费| 99久久亚洲综合精品成人| 午夜天堂av天堂久久久| 久久久青草青青国产亚洲免观| 人妻少妇久久中文字幕| 国产精品一区二区久久精品涩爱| 91久久精品91久久性色| 久久精品国产久精国产果冻传媒 | 狠狠色婷婷久久一区二区 | 性做久久久久久久久老女人| 国产情侣久久久久aⅴ免费| 综合久久精品色| 欧美久久久久久午夜精品| 国产精品免费久久久久久久久 | 国产一区二区精品久久| 久久人妻少妇嫩草AV无码专区| 久久精品人人做人人爽电影| 久久福利片| 国产无套内射久久久国产| 国产日韩久久免费影院| 精品国产婷婷久久久| 久久久久香蕉视频| 亚洲а∨天堂久久精品| 久久强奷乱码老熟女网站| 久久久99精品成人片中文字幕| 精品久久久久久无码中文字幕| 伊人丁香狠狠色综合久久| 夜夜亚洲天天久久|