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

               C++ 技術中心

               :: 首頁 :: 聯系 ::  :: 管理
              160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

            公告

            鄭重聲明:本BLOG所發表的原創文章,作者保留一切權利。必須經過作者本人同意后方可轉載,并注名作者(天空)和出處(CppBlog.com)。作者Email:coder@luckcoder.com

            留言簿(27)

            搜索

            •  

            最新隨筆

            最新評論

            評論排行榜

            幾天前,在CSDN論壇看到這么一則討論:在宏定義中怎么使用可變參數?(http://expert.csdn.net/Expert/topic/2925/2925165.xml)。樓主希望能定義這樣的macro:

            #define fun1(a, b, ...) fun2(__FILE__, __LINE__, a, b, ...)

            我猜樓主是想寫trace,如果不能使用可變參數的macro,那么就得像MFC那樣寫一堆TRACE macros:


            // 取自 MFC 7.1 的 afx.h
            // The following trace macros are provided for backward compatiblity
            // (they also take a fixed number of parameters which provides
            // some amount of extra error checking)
            #define TRACE0(sz) TRACE(_T("%s"), _T(sz))
            #define TRACE1(sz, p1) TRACE(_T(sz), p1)
            #define TRACE2(sz, p1, p2) TRACE(_T(sz), p1, p2)
            #define TRACE3(sz, p1, p2, p3) TRACE(_T(sz), p1, p2, p3)

            太丑陋了!還好,C99標準支持Variadic Macros,在GCC中,可以這么寫:

            // http://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html
            #define debug(format, ...) fprintf(stderr, format, __VA_ARGS__)

            還可以順便打印文件名和行號:

            #define debug(format, ...) do {/
            fprintf(stderr, "%s (%d): ", __FILE__, __LINE__);/
            fprintf(stderr, format, __VA_ARGS__);/
            } while (0)

            但可惜Visual C++ 7.1還不支持這項功能:( 不過我們在C++中至少可以繞彎解決,做到既能自動記錄文件名和行號,又能使用變長參數調用。這個辦法不是我獨創的,實際上ATL的atltrace.h中就有它的實現(CtraceFileAndLineInfo class),我在Code Project也找到了相同的實現(http://www.codeproject.com/debug/location_trace.asp),甚至在CUJ的C++ Experts Forum 也能看到相近的做法(http://www.cuj.com/documents/s=8250/cujcexp2106alexandr/),當然Alexandrescu的辦法技巧性更強。

            思路:寫一個重載了 operator() 的class,令 TRACE 宏返回該class的一個object:

            #include
            #include

            #ifndef NDEBUG // debug mode

            class tracer
            {
            public:
            tracer(const char* file, int line)
            : file_(file), line_(line)
            {}

            void operator()(const char* fmt, ...)
            {
            va_list ap;

            // print the file name and line number
            fprintf(stderr, "%s (%d): ", file_, line_);

            va_start(ap, fmt);
            vfprintf(stderr, fmt, ap);
            va_end(ap);

            fprintf(stderr, "/r/n"); // print the new-line character
            }

            private:
            // copy-ctor and operator=
            tracer(const tracer&);
            tracer& operator=(const tracer&);

            private:
            const char* file_;
            int line_;
            };
            #define TRACE (tracer(__FILE__, __LINE__))
            #else // NDEBUG
            #define TRACE (void)
            #endif // NDEBUG

            int main()
            {
            #ifndef NDEBUG
            tracer(__FILE__, __LINE__)("%x", 123);
            #endif

            TRACE("%s", "Happy debugging.");
            }

            這樣做是multithreading-safe的。G++ 3.3.1 / Visual C++ 7.1 / Borland C++ 5.5.1 通過。

            posted on 2013-08-19 11:03 C++技術中心 閱讀(1823) 評論(1)  編輯 收藏 引用 所屬分類: C++ 基礎

            Feedback

            # re: 可變參數[未登錄] 2013-08-20 13:05 xu
            此tracer的設計確實十分精妙!  回復  更多評論
              

            久久强奷乱码老熟女| 久久乐国产综合亚洲精品| 久久综合九色综合网站| 亚洲乱亚洲乱淫久久| 亚洲天堂久久久| 精品九九久久国内精品| 色综合久久夜色精品国产| 国产成人精品白浆久久69| 要久久爱在线免费观看| 狠狠色丁香久久综合五月| 亚洲欧美久久久久9999| 久久九九有精品国产23百花影院| 免费精品久久天干天干| 久久99精品久久久久久齐齐| 国产成人久久AV免费| 亚洲精品无码久久一线| 欧美亚洲国产精品久久久久| 久久久久亚洲AV成人网人人网站| 69久久精品无码一区二区| 日本久久久久亚洲中字幕| 一本久久免费视频| 亚洲精品高清一二区久久| 国产精品成人精品久久久| 久久不射电影网| 久久亚洲中文字幕精品有坂深雪| 亚洲伊人久久精品影院| 国色天香久久久久久久小说| 2021国内精品久久久久久影院| 久久免费视频一区| 伊人久久五月天| 亚洲AV无码1区2区久久| 日本强好片久久久久久AAA | 久久精品国产亚洲AV无码娇色| 亚洲色大成网站WWW久久九九| 久久精品国产乱子伦| 色妞色综合久久夜夜| 久久亚洲中文字幕精品有坂深雪| 久久亚洲日韩精品一区二区三区| 奇米影视7777久久精品| 嫩草影院久久国产精品| 久久久久免费视频|