• <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 閱讀(25005) 評論(0)  編輯 收藏 引用
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(8)

            隨筆檔案(16)

            鏈接

            搜索

            •  

            積分與排名

            • 積分 - 198340
            • 排名 - 134

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久亚洲av无码专区导航| 99久久国产宗和精品1上映| 99久久无色码中文字幕| 人人狠狠综合久久亚洲婷婷| 久久久久综合国产欧美一区二区| 亚洲精品国精品久久99热| 97久久婷婷五月综合色d啪蜜芽 | 亚洲精品成人网久久久久久| 久久精品aⅴ无码中文字字幕不卡| 久久精品www人人爽人人| 久久久久亚洲AV综合波多野结衣| 久久亚洲精品无码aⅴ大香| 久久精品aⅴ无码中文字字幕不卡| Xx性欧美肥妇精品久久久久久 | 久久乐国产精品亚洲综合| 日韩av无码久久精品免费| 久久久久久无码国产精品中文字幕 | 91久久国产视频| 无码精品久久一区二区三区 | 99久久99久久精品国产| 亚洲中文字幕无码久久精品1| 久久99精品久久久久久噜噜| 色欲综合久久躁天天躁蜜桃| 免费精品久久久久久中文字幕| 国产精品久久久久久| 久久九九精品99国产精品| 99久久精品免费看国产一区二区三区 | 久久午夜伦鲁片免费无码| 伊人久久大香线蕉AV一区二区| 精品久久久久中文字幕一区| 久久不射电影网| 久久精品国产影库免费看| 久久精品www人人爽人人| 久久综合给合久久狠狠狠97色69| 国产成人精品综合久久久| 综合久久精品色| 色天使久久综合网天天| 无码人妻久久一区二区三区蜜桃| 久久天天婷婷五月俺也去| 久久精品中文无码资源站| 三级韩国一区久久二区综合|