Re: [問題] C++用marco debug的寫法

看板C_and_CPP作者時間14年前 (2011/04/06 23:41), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串3/4 (看更多)
有點離題 只是有點好奇 Null & operator <<( std::ostream & (*)( std::ostream & ) ) 這個為什麼不能寫成 template <typename T> Null & operator <<( T & (*)( T & ) ) 如果改這樣就會不能編繹 很奇怪 ※ 引述《legnaleurc (CA)》之銘言: : ※ 引述《sudada (嘰咕嘰咕嘰咕)》之銘言: : : 所以請教有沒有比較好的寫法 謝謝 : 搞一個空的輸出就好了吧 ... : class Null { : public: : // 為了 std::endl : Null & operator <<( std::ostream & (*)( std::ostream & ) ) { : return *this; : } : // 其他的都來這裡 : template< typename T > : Null & operator <<( const T & ) { : return *this; : } : }; : #ifdef DEBUG : # define _DEBUG std::cerr : #else : extern Null null; : # define _DEBUG null : #endif : 然後 : _DEBUG << "const char *" << 1 << 1.5 << std::endl; : 都可以丢給 null 或 cerr : 注意 std 不能省 ... 誰知道 source 有沒有 using namespace : overhead 應該是不會太多 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.160.33.110

04/06 23:48, , 1F
應該是因為 endl 本身就是個 template function
04/06 23:48, 1F

04/06 23:48, , 2F
都用 template 它推導不出來
04/06 23:48, 2F

04/07 02:43, , 3F
VC 給 C2914 原因應該就如一樓所言沒錯
04/07 02:43, 3F

04/07 02:45, , 5F
樓上不用睡覺
04/07 02:45, 5F

04/07 19:24, , 6F
原來如此
04/07 19:24, 6F
文章代碼(AID): #1Dd8cHY6 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1Dd8cHY6 (C_and_CPP)