• <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 閱讀(25028) 評論(0)  編輯 收藏 引用
            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(8)

            隨筆檔案(16)

            鏈接

            搜索

            •  

            積分與排名

            • 積分 - 198662
            • 排名 - 134

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            色欲久久久天天天综合网| 97视频久久久| 久久精品无码一区二区三区免费 | 国产一区二区三区久久精品| 久久婷婷五月综合成人D啪| 日韩精品久久久肉伦网站 | 久久久精品人妻无码专区不卡| 日本久久中文字幕| 精品国际久久久久999波多野| 久久久久九九精品影院| 久久精品国产久精国产果冻传媒| 国产精品一久久香蕉国产线看观看| 99久久人人爽亚洲精品美女| 狠狠色丁香久久婷婷综合图片| 国产精品一区二区久久不卡 | 精产国品久久一二三产区区别 | 综合久久一区二区三区 | 久久国产免费直播| 久久精品www人人爽人人| 久久国产AVJUST麻豆| 国产午夜精品久久久久免费视| 久久精品国产亚洲av瑜伽| 精品久久久久久亚洲| 国产精品中文久久久久久久| 精品久久人人妻人人做精品| 99久久精品毛片免费播放| 亚洲伊人久久精品影院| 亚洲国产精品成人久久蜜臀| 精品久久久久久久中文字幕| 日本福利片国产午夜久久| 久久久精品人妻一区二区三区蜜桃 | 国产亚洲欧美精品久久久| 亚洲中文精品久久久久久不卡 | 亚洲狠狠婷婷综合久久蜜芽| 久久久久久久91精品免费观看| 久久亚洲精品无码播放| 亚洲国产成人久久精品影视| 69久久精品无码一区二区| 狠狠色丁香婷婷久久综合不卡| 精品久久久久久久无码| 久久精品国产精品青草|