久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <small id='0oF9d'></small><noframes id='0oF9d'>

    <i id='0oF9d'><tr id='0oF9d'><dt id='0oF9d'><q id='0oF9d'><span id='0oF9d'><b id='0oF9d'><form id='0oF9d'><ins id='0oF9d'></ins><ul id='0oF9d'></ul><sub id='0oF9d'></sub></form><legend id='0oF9d'></legend><bdo id='0oF9d'><pre id='0oF9d'><center id='0oF9d'></center></pre></bdo></b><th id='0oF9d'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0oF9d'><tfoot id='0oF9d'></tfoot><dl id='0oF9d'><fieldset id='0oF9d'></fieldset></dl></div>
  1. <tfoot id='0oF9d'></tfoot>

    <legend id='0oF9d'><style id='0oF9d'><dir id='0oF9d'><q id='0oF9d'></q></dir></style></legend>

      • <bdo id='0oF9d'></bdo><ul id='0oF9d'></ul>

      使用 SFINAE 檢查全局操作符<<?

      Using SFINAE to check for global operatorlt;lt;?(使用 SFINAE 檢查全局操作符?)

        <small id='BnZGa'></small><noframes id='BnZGa'>

          <i id='BnZGa'><tr id='BnZGa'><dt id='BnZGa'><q id='BnZGa'><span id='BnZGa'><b id='BnZGa'><form id='BnZGa'><ins id='BnZGa'></ins><ul id='BnZGa'></ul><sub id='BnZGa'></sub></form><legend id='BnZGa'></legend><bdo id='BnZGa'><pre id='BnZGa'><center id='BnZGa'></center></pre></bdo></b><th id='BnZGa'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='BnZGa'><tfoot id='BnZGa'></tfoot><dl id='BnZGa'><fieldset id='BnZGa'></fieldset></dl></div>
            • <legend id='BnZGa'><style id='BnZGa'><dir id='BnZGa'><q id='BnZGa'></q></dir></style></legend>
                <bdo id='BnZGa'></bdo><ul id='BnZGa'></ul>

                  <tbody id='BnZGa'></tbody>

                <tfoot id='BnZGa'></tfoot>
                本文介紹了使用 SFINAE 檢查全局操作符<<?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                我想要幾個重載的全局 to_string() 函數,它們采用某種類型的 T 并將其轉換為它的字符串表示形式.對于一般情況,我希望能夠寫:

                I want to have several overloaded, global to_string() functions that take some type T and convert it to its string representation. For the general case, I want to be able to write:

                template<typename T,class OutputStringType> inline
                typename enable_if<!std::is_pointer<T>::value
                                && has_insertion_operator<T>::value,
                                   void>::type
                to_string( T const &t, OutputStringType *out ) {
                  std::ostringstream o;
                  o << t;
                  *out = o.str();
                }
                

                到目前為止,我對 has_insertion_operator 的實現是:

                My implementation of has_insertion_operator so far is:

                struct sfinae_base {
                  typedef char yes[1];
                  typedef char no[2];
                };
                
                template<typename T>
                struct has_insertion_operator : sfinae_base {
                  template<typename U> static yes& test( U& );
                  template<typename U> static no& test(...);
                
                  static std::ostream &s;
                  static T const &t;
                
                  static bool const value = sizeof( test( s << t ) ) == sizeof( yes ); // line 48
                };
                

                (它借鑒了this和這個.)這似乎有效.但是現在我想要一個 to_string 的重載版本,用于 notoperator<<<do 的類型> 有自己的to_string() member 函數,即:

                (It borrows from this and this.) That seems to work. But now I want to have an overloaded version of to_string for types that do not have operator<< but do have their own to_string() member function, i.e.:

                template<class T,class OutputStringType> inline
                typename enable_if<!has_insertion_operator<T>::value
                                && has_to_string<T,std::string (T::*)() const>::value,
                                   void>::type
                to_string( T const &t, OutputStringType *out ) {
                  *out = t.to_string();
                }
                

                has_to_string 的實現是:

                #define DECL_HAS_MEM_FN(FN_NAME)                                      
                  template<typename T,typename S>                                     
                  struct has_##FN_NAME : sfinae_base {                                
                    template<typename SignatureType,SignatureType> struct type_check; 
                    template<class U> static yes& test(type_check<S,&U::FN_NAME>*);   
                    template<class U> static no& test(...);                           
                    static bool const value = sizeof( test<T>(0) ) == sizeof( yes );  
                  }
                
                DECL_HAS_MEM_FN( to_string );
                

                (這部分似乎工作正常.它改編自 this.)但是,當我有:

                (This part seems to work fine. It's adapted from this.) However, when I have:

                struct S {
                  string to_string() const {
                    return "42";
                  }
                };
                
                int main() {
                  string buf;
                  S s;
                  to_string( s, &buf ); // line 104
                }
                

                我明白了:

                foo.cpp: In instantiation of ‘const bool has_insertion_operator<S>::value’:
                foo.cpp:104:   instantiated from here
                foo.cpp:48: error: no match for ‘operator<<’ in ‘has_insertion_operator<S>::s << has_insertion_operator<S>::t’
                

                似乎 SFINAE 沒有發生.如何正確編寫 has_insertion_operator 以便確定全局 operator<< 是否可用?

                It seems like SFINAE is not happening. How do I write has_insertion_operator correctly such that it determines whether a global operator<< is available?

                僅供參考:我使用的是 g++ 4.2.1(在 Mac OS X 上作為 Xcode 的一部分提供).另外,我希望代碼只是標準的 C++03,沒有 3rd 方庫,例如 Boost.

                FYI: I'm using g++ 4.2.1 (that which ships as part of Xcode on Mac OS X). Also, I'd like the code to be only standard C++03 without 3rd-party libraries, e.g., Boost.

                謝謝!

                推薦答案

                我應該更忠實于這個的答案.一個有效的實現是:

                I should have simply been more faithful to this answer. A working implementation is:

                namespace has_insertion_operator_impl {
                  typedef char no;
                  typedef char yes[2];
                
                  struct any_t {
                    template<typename T> any_t( T const& );
                  };
                
                  no operator<<( std::ostream const&, any_t const& );
                
                  yes& test( std::ostream& );
                  no test( no );
                
                  template<typename T>
                  struct has_insertion_operator {
                    static std::ostream &s;
                    static T const &t;
                    static bool const value = sizeof( test(s << t) ) == sizeof( yes );
                  };
                }
                
                template<typename T>
                struct has_insertion_operator :
                  has_insertion_operator_impl::has_insertion_operator<T> {
                };
                

                我相信它實際上依賴于 SFINAE.

                I believe that it does not actually rely on SFINAE.

                這篇關于使用 SFINAE 檢查全局操作符<<?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Why do two functions have the same address?(為什么兩個函數的地址相同?)
                Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                mixing templates with polymorphism(混合模板與多態性)
                When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)

                <legend id='6WRK5'><style id='6WRK5'><dir id='6WRK5'><q id='6WRK5'></q></dir></style></legend>
              • <small id='6WRK5'></small><noframes id='6WRK5'>

                  <bdo id='6WRK5'></bdo><ul id='6WRK5'></ul>

                • <i id='6WRK5'><tr id='6WRK5'><dt id='6WRK5'><q id='6WRK5'><span id='6WRK5'><b id='6WRK5'><form id='6WRK5'><ins id='6WRK5'></ins><ul id='6WRK5'></ul><sub id='6WRK5'></sub></form><legend id='6WRK5'></legend><bdo id='6WRK5'><pre id='6WRK5'><center id='6WRK5'></center></pre></bdo></b><th id='6WRK5'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6WRK5'><tfoot id='6WRK5'></tfoot><dl id='6WRK5'><fieldset id='6WRK5'></fieldset></dl></div>

                          <tbody id='6WRK5'></tbody>
                        <tfoot id='6WRK5'></tfoot>
                        • 主站蜘蛛池模板: 成人久久久 | 美女久久久久久久 | 99热首页 | 国产精品视频在线播放 | 国产精品99999999| 国产小视频自拍 | 综合久久综合久久 | 一区二区三区四区在线视频 | 精品91久久 | 欧美黑人一级爽快片淫片高清 | 国产高清一二三区 | 日本特黄a级高清免费大片 国产精品久久性 | 日韩精品久久久久 | 亚洲一区久久 | 国产视频不卡一区 | 国产精品3区 | 男女视频在线观看 | 久久久久一区二区三区四区 | 999国产视频 | 91免费看片 | 国产午夜精品一区二区三区四区 | 国产精品视频导航 | 国产在线观看一区二区 | 成人av免费在线观看 | 日韩精品一区二区三区视频播放 | 日韩二区 | 国产欧美精品一区二区三区 | 99爱在线免费观看 | 日韩中文视频 | 日韩高清不卡 | 国产精品一区二区三区久久 | 国产精品一区二区三区久久 | 欧美综合久久久 | 9191成人精品久久 | 国产精品久久久亚洲 | 3级毛片 | 欧美xxxx在线 | 97av在线 | av在线免费网站 | 亚洲国产欧美日韩 | 性高湖久久久久久久久 |