• <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>
            posts - 16,  comments - 34,  trackbacks - 0

            一、 介紹預定義宏"__cplusplus"

                    一.1 __cplusplus宏在C++標準中的描述如下:

            16.8 Predefined macro names
            __cplusplus The name __cplusplus is defined to the value 199711L when compiling a C++ translation unit.143)
            143) It is intended that future versions of this standard will replace the value of this macro with a greater value.
            Non-conforming compilers should use a value with at most five decimal digits.

                                                    —— ISO C++03 p315
                                                    —— ISO C++98 p309

            如果一段代碼是需要針對C++編寫的, 可以使用該宏進行條件編譯。


                    一.2 __cplusplus的值是為了表示C++的版本
                    一.3 __cplusplus的類型是"long int"


            ——這是理論上的。
            現在發布的C++標準只有C++98, C++03是C++98的修訂, 內容只有很少改變。
            所以在ISO C++03中, 也規定該宏的值是199711L(這是一個長整數字面值)。

            ——實際上, 目前不應該依賴宏的值, 因為:
            1. 目前標準中規定的值只有一個——199711L, 沒有根據該值進行條件編譯的可能。

            2. 目前C++編譯器并不一定按標準實現這個宏(見測試)。



            二、 測試預定義宏__cplusplus

            示例1:
            #include <stdio.h>

            int main() {

            #define TO_LITERAL(text) TO_LITERAL_(text)
            #define TO_LITERAL_(text) #text
            #ifndef __cplusplus
            /* this translation unit is being treated as a C one */
                printf(
            "a C program\n");
            #else
            // this translation unit is being treated as a C++ one
                printf("a C++ program\n__cplusplus expands to \""
                      TO_LITERAL(__cplusplus) "\"\n");
            #endif
                (
            void)getchar();
                
            return 0;
            }

            代碼很簡單:
            如果沒有定義__cplusplus, 那么當前源代碼被當作C源代碼處理。
            如果定義了__cplusplus,那么當前源代碼被當中C++源代碼處理, 并且輸出__cplusplus宏被展開后的字符串。


            示例2:

            __cplusplus

            這段代碼更簡單了, 只是使用了__cplusplus宏。
            然后查看預處理(見《查看源文件預處理結果》),看其被擴展后的結果。



            三、 測試結果

                    ——msvc8
                    ——msvc9
            __cplusplus按照標準被擴展為——199711L。

                    ——msvc6
                    ——gcc (GCC) 3.4.2 (mingw-special)
                    ——gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
            __cplusplus只是簡單的被擴展為——1。


            這也說明了不應該依賴__cplusplus宏的值。



            相關鏈接:

            ——源代碼
            http://immature.googlecode.com/svn/trunk/iMmature/sample/predefined_macro/__cplusplus

            ——《查看源文件預處理結果
            http://www.shnenglu.com/ownwaterloo/archive/2009/04/16/get_result_of_preprocessing.html



            Creative Commons License
            作品采用知識共享署名-非商業性使用-相同方式共享 2.5 中國大陸許可協議進行許可。

            轉載請注明 :
            文章作者 - OwnWaterloo
            發表時間 - 2009年04月20日
            原文鏈接 - http://www.shnenglu.com/ownwaterloo/archive/2009/04/20/predefined_macro___cplusplus.html
            posted on 2009-04-20 14:42 OwnWaterloo 閱讀(25027) 評論(0)  編輯 收藏 引用
            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(8)

            隨筆檔案(16)

            鏈接

            搜索

            •  

            積分與排名

            • 積分 - 198659
            • 排名 - 134

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            色偷偷88888欧美精品久久久| 一级做a爱片久久毛片| 超级97碰碰碰碰久久久久最新| 久久大香萑太香蕉av| 国产精品对白刺激久久久| 一本久久久久久久| 久久无码国产专区精品| 精品久久久久久亚洲精品 | 亚洲人成网站999久久久综合| 中文字幕无码精品亚洲资源网久久| 久久综合综合久久97色| 欧美日韩久久中文字幕| 久久99精品免费一区二区| 色偷偷偷久久伊人大杳蕉| 2021国内久久精品| 热久久国产欧美一区二区精品| 久久综合亚洲欧美成人| 久久毛片一区二区| 久久亚洲国产精品123区| 2021精品国产综合久久| 国内精品人妻无码久久久影院导航| 久久最近最新中文字幕大全| 欧美牲交A欧牲交aⅴ久久| 亚洲中文字幕伊人久久无码| 日本精品一区二区久久久| 国产福利电影一区二区三区久久久久成人精品综合 | 久久性生大片免费观看性| 久久精品成人免费网站| 久久国产精品99精品国产| 麻豆AV一区二区三区久久| 亚洲午夜久久久影院| 国产A级毛片久久久精品毛片| 一本一道久久a久久精品综合| 日产久久强奸免费的看| 伊人色综合九久久天天蜜桃| 亚洲一级Av无码毛片久久精品| 久久青青草原亚洲av无码| 久久天天躁狠狠躁夜夜2020老熟妇 | 久久久久久A亚洲欧洲AV冫| 91麻豆精品国产91久久久久久| 久久99国产精品久久久|