• <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>

            兔子的技術博客

            兔子

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              202 Posts :: 0 Stories :: 43 Comments :: 0 Trackbacks

            留言簿(10)

            最新評論

            閱讀排行榜

            評論排行榜

            C++0x引入了新的關鍵字decltype,它是一個操作符,用來取得表達式的類型,主要在泛型編程中使用。這里,簡單介紹一下語法規則。

            語法形式:decltype (expression)
            其中,這里的括號必不可少(這點不同于sizeof操作符)。decltype(e)可看到是一個類型別名,并且不會對表達式e進行計算(即只有編譯時行為而無運行時行為)。另外,不允許把decltype作用于一個類型,因為沒有任何理由要這樣做。

            確定decltype(e)類型的規則如下
            Rule-1. 如果e是一個標識符表達式或者類成員訪問表達式,那么decltype(e)就是e所命名的實體的類型。如果沒有此實體或者e命名了一個重載函數集,那么程序是ill-formed的。
            Rule-2. 如果e是一個函數調用或者一個重載操作符調用(忽略e外面的括號),那么decltype(e)就是該函數的返回類型。
            Rule-3. 否則,假設e的類型是T:如果e是一個左值,則decltype(e)就是T&;否則(e是一個右值),decltype(e)就是T。

            舉例分析如下(內容來自參考Ref1):

            eg1 名字空間或局部作用域內的變量(Rule-1)
            int a;
            int& b = a;
            const int& c = a;
            const int d = 5;
            const A e;

            (注:不能直接編譯,這里寫出來只是分析)
            decltype(a) // int 
            decltype(b) // int&
            decltype(c) // const int&
            decltype(d) // const int
            decltype(e) // const A

            但需要注意括號可能會影響結果,例如:
            decltype((a));  // int& (此時(a)表達式不滿足Rule-1和Rule-2,應用Rule-3,而表達式(a)是一個左值,所以為int&)

            eg2 函數形參(Rule-1)
            void foo(int a, int& b, float&& c, int* d)
            {
                decltype(a) // int
                decltype(b) // int&
                decltype(c) // float&&
                decltype(d) // int*
            }

            eg3 函數類型(Rule-1)
            int foo(char);
            int bar(char);
            int bar(int);

            decltype(foo) // int(char)
            decltype(bar) // error, bar is overloaded

            但需要注意當形成函數指針時適用Rule-3:
            decltype(&foo) // int(*)(char)
            decltype(*&foo) // int(&)(char)

            eg4 數據類型(Rule-1)
            int a[10];
            decltype(a)  // int[10]

            eg5 成員變量(Rule-1)
            class A {
                int a;
                int& b;
                static int c;
                
                void foo() {
                    decltype(a)          // int
                    decltype(this->a)    // int
                    decltype((*this).a)  // int
                    decltype(b)          // int&
                    decltype(c)          // int (static members are treated as variables in namespace scope)
                }
                void bar() const {
                    decltype(a)   // int
                    decltype(b)   // int&
                    decltype(c)   // int
                }
            };

            A aa;
            const A& caa = aa;
            decltype(aa.a)  // int
            decltype(aa.b)   // int&
            decltype(caa.a)  // int

            內置操作符.*和->*適用Rule-3
            decltype(aa.*&A::a) // int&
            decltype(aa.*&A::b) // illegal, cannot take the address of a reference member
            decltype(caa.*&A::a) // const int&

            eg6 this(Rule-3)
            class X {
                void foo() {
                    decltype(this)    // X*,因為this是右值
                    decltype(*this)   // X&,因為*this是左值
                }
                void bar() const {
                    decltype(this)   // const X*
                    decltype(*this)  // const X&
                }
            };

            eg7 指向成員變量和成員函數的指針(Rule-1)
            class A {
                int x;
                int& y;
                int foo(char);
                int& bar() const;
            };

            decltype(&A::x)    // int A::*
            decltype(&A::y)    // error: pointers to reference members are disallowed (8.3.3 (3))
            decltype(&A::foo) // int (A::*) (char)
            decltype(&A::bar) // int& (A::*) () const

            eg8 字面值(Rule-3)
            (字符串字面值是左值,其它字面值都是右值)
            decltype("decltype") // const char(&)[9]
            decltype(1) // int

            eg9 冗余的引用符(&)和CV修飾符
            由于decltype表達式是一個類型別名,因此冗余的引用符(&)和CV修飾符被忽略:
            int& i = ...;
            const int j = ...;
            decltype(i)&         // int&. The redundant & is ok
            const decltype(j)   // const int. The redundant const is ok

            eg10 函數調用(Rule-2)
            int foo();
            decltype(foo())    // int
            float& bar(int);
            decltype (bar(1))  // float&
            class A { ... };
            const A bar();
            decltype (bar())    // const A
            const A& bar2();
            decltype (bar2())  // const A&

            eg11 內置操作符(Rule-3)
            decltype(1+2)     // int (+ returns an rvalue)
            int* p;
            decltype(*p)        // int& (* returns an lvalue)
            int a[10];
            decltype(a[3]);     // int& ([] returns an lvalue)
            int i; int& j = i;
            decltype (i = 5)   // int&, because assignment to int returns an lvalue
            decltype (j = 5)   // int&, because assignment to int returns an lvalue
            decltype (++i);    // int&
            decltype (i++);    // int (rvalue)

            如何用程序驗證decltype的結果?可以參考下面的程序對上面的分析結果進行驗證:
            F:\tmp>type decltype_eg1.cpp
            #include <iostream>
            #include <string>
            using namespace std;

            template <typename T>
            string Foo()
            {
                return "unknown";
            }

            template <>
            string Foo<int>()
            {
                return "int";
            }

            template <>
            string Foo<const int>()
            {
                return "const int";
            }

            template <>
            string Foo<int &>()
            {
                return "int&";
            }

            template <>
            string Foo<const int&>()
            {
                return "const int&";
            }

            class A{};

            template <>
            string Foo<A>()
            {
                return "A";
            }

            int main()
            {
                int a;
                int &b = a;
                const int &c = a;
                const int d = 5;
                A e;
                double f;

                cout << "a: " << Foo<decltype(a)>() << endl;
                cout << "b: " << Foo<decltype(b)>() << endl;
                cout << "c: " << Foo<decltype(c)>() << endl;
                cout << "d: " << Foo<decltype(d)>() << endl;
                cout << "e: " << Foo<decltype(e)>() << endl;
                cout << "f: " << Foo<decltype(f)>() << endl;
            }


            F:\tmp>g++ decltype_eg1.cpp -std=c++0x

            F:\tmp>a.exe
            a: int
            b: int&
            c: const int&
            d: const int
            e: A
            f: unknown

            F:\tmp>gcc --version
            gcc (GCC) 4.3.0 20080305 (alpha-testing) mingw-20080502
            Copyright (C) 2008 Free Software Foundation, Inc.
            This is free software; see the source for copying conditions.  There is NO
            warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


            參考資料:
            Ref1: Decltype (revision 6): proposed wording
            Ref2: Decltype (revision 7): proposed wording


            轉自:http://www.cublog.cn/u/18517/showart_1664016.html
            posted on 2011-06-23 08:25 會飛的兔子 閱讀(1721) 評論(0)  編輯 收藏 引用 所屬分類: C++及開發環境
            久久人人爽人人爽人人AV| 久久精品一区二区影院| 久久精品国产亚洲av麻豆色欲| 老男人久久青草av高清| 亚洲色婷婷综合久久| 亚洲天堂久久精品| 久久久久久久91精品免费观看 | 亚洲精品美女久久777777| 国产综合久久久久久鬼色| 久久久久亚洲AV无码专区桃色| 久久无码AV中文出轨人妻| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 欧美亚洲另类久久综合| 久久99精品久久久大学生| 波多野结衣中文字幕久久| 久久国产免费直播| 97热久久免费频精品99| 久久精品国产男包| 久久久精品国产亚洲成人满18免费网站 | 久久偷看各类wc女厕嘘嘘| 精品久久久久中文字幕一区| 久久午夜羞羞影院免费观看| 亚洲国产精品一区二区三区久久| 91久久精品91久久性色| 久久精品国产久精国产果冻传媒 | 欧美伊人久久大香线蕉综合69| 日韩精品久久无码中文字幕| 亚洲国产成人精品女人久久久 | 日韩亚洲国产综合久久久| 91精品国产91热久久久久福利| 久久久久久国产精品无码下载 | 久久人人爽人人爽人人AV东京热| 亚洲精品NV久久久久久久久久| 亚洲国产精品人久久| 久久久久国产一级毛片高清版| 亚洲av日韩精品久久久久久a| 一本大道久久东京热无码AV| 日韩十八禁一区二区久久| 伊人久久国产免费观看视频| 国产69精品久久久久APP下载| 看全色黄大色大片免费久久久|