• <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 閱讀(25007) 評論(0)  編輯 收藏 引用
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(8)

            隨筆檔案(16)

            鏈接

            搜索

            •  

            積分與排名

            • 積分 - 198363
            • 排名 - 134

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲va久久久噜噜噜久久狠狠| 66精品综合久久久久久久| 性做久久久久久久久老女人| 老司机午夜网站国内精品久久久久久久久| 久久久久99精品成人片三人毛片| 中文字幕久久精品无码| 久久综合给合久久国产免费| 99久久精品久久久久久清纯| 蜜桃麻豆WWW久久囤产精品| 久久久综合九色合综国产| 久久黄视频| 国产成人综合久久综合| 久久久久亚洲国产| 久久人人妻人人爽人人爽| 99久久婷婷国产一区二区| 精品国产乱码久久久久久呢 | 久久国产精品国语对白| 久久夜色精品国产噜噜麻豆| 久久精品中文字幕第23页| 久久99国产精品久久99| 亚洲国产精品18久久久久久| 少妇久久久久久被弄到高潮| 99久久www免费人成精品| 国产精品久久影院| 精品多毛少妇人妻AV免费久久| 国产成人香蕉久久久久| 久久se精品一区二区| 97久久天天综合色天天综合色hd| 中文字幕无码av激情不卡久久| 久久精品夜色噜噜亚洲A∨| 老司机国内精品久久久久| 国产午夜精品理论片久久影视| 99久久精品午夜一区二区| 99蜜桃臀久久久欧美精品网站 | 国产亚洲欧美精品久久久| 中文字幕日本人妻久久久免费| 久久综合偷偷噜噜噜色| 欧美黑人激情性久久| 中文字幕无码免费久久| 日韩乱码人妻无码中文字幕久久| 午夜精品久久久久久99热|