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

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

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

      <tfoot id='gUIRz'></tfoot>

    2. <legend id='gUIRz'><style id='gUIRz'><dir id='gUIRz'><q id='gUIRz'></q></dir></style></legend>

        • <bdo id='gUIRz'></bdo><ul id='gUIRz'></ul>

        如何在檢測習語中要求精確的函數簽名?

        How to require an exact function signature in the detection idiom?(如何在檢測習語中要求精確的函數簽名?)

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

          <small id='8vhTl'></small><noframes id='8vhTl'>

          <tfoot id='8vhTl'></tfoot>
          • <bdo id='8vhTl'></bdo><ul id='8vhTl'></ul>

                  本文介紹了如何在檢測習語中要求精確的函數簽名?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  假設我有一個 T 類型,我想檢測它是否有一個下標運算符,我可以用另一個類型 Index 調用它.以下示例工作正常:

                  Let's suppose I have a type T and I want to detect whether it has a subscript operator which I can call with with another type Index. The following example works just fine:

                  #include <type_traits>
                  #include <vector>
                  
                  template < typename T, typename Index >
                  using subscript_t = decltype(std::declval<T>()[std::declval<Index>()]);
                  
                  int main()
                  {
                      using a = subscript_t< std::vector<int>, size_t >;
                      using b = subscript_t< std::vector<int>, int    >;
                  }
                  

                  但是,我希望當且僅當函數簽名完全匹配時才能檢測到該函數.在上面的例子中,我想要語句 subscript_t<std::vector<int>, int >; 拋出類似no可靠重載運算符[]的錯誤,因為std::vector的下標運算符的簽名

                  However, I want the function to be detected if and only if the function signature matches exactly. In the example above I would like the statement subscript_t< std::vector<int>, int >; to throw an error like no viable overloaded operator[], because the signature of the subscript operator for std::vector is

                  std::vector<T, std::allocator<T>>::operator[](size_type pos);
                  

                  其中 size_type 在 GCC 中是 unsigned long.如何避免從 intsize_t 的隱式轉換發生?

                  where size_type in GCC is unsigned long. How can I avoid the implicit conversion from int to size_t to take place?

                  推薦答案

                  With is_detected,你可以這樣做:

                  With is_detected, you may do:

                  template <typename T, typename Ret, typename Index>
                  using subscript_t = std::integral_constant<Ret (T::*) (Index), & T::operator[]>;
                  
                  
                  template <typename T, typename Ret, typename Index>
                  using has_subscript = is_detected<subscript_t, T, Ret, Index>;
                  
                  static_assert(has_subscript<std::vector<int>, int&, std::size_t>::value, "!");
                  static_assert(!has_subscript<std::vector<int>, int&, int>::value, "!");
                  

                  演示

                  我在 SO 文檔中寫了這個:

                  I wrote this in SO Documentation:

                  概括type_trait的創建:基于SFINAE有實驗特征detected_ordetected_tis_detected.

                  To generalize type_trait creation:based on SFINAE there are experimental traits detected_or, detected_t, is_detected.

                  帶模板參數typename Defaulttemplate Optypename ... Args:

                  • is_detected:std::true_typestd::false_type 的別名,取決于 Op
                  • detected_t:Opnonesuch 的別名,取決于 Op.
                  • detected_or:value_tis_detectedtype 的結構的別名OpDefault 取決于 Op
                  • 的有效性
                  • is_detected: alias of std::true_type or std::false_type depending of the validity of Op<Args...>
                  • detected_t: alias of Op<Args...> or nonesuch depending of validity of Op<Args...>.
                  • detected_or: alias of a struct with value_t which is is_detected, and type which is Op<Args...> or Default depending of validity of Op<Args...>

                  可以使用 std::void_t 為 SFINAE 實現如下:

                  which can be implemented using std::void_t for SFINAE as following:

                  namespace detail {
                      template <class Default, class AlwaysVoid,
                                template<class...> class Op, class... Args>
                      struct detector
                      {
                          using value_t = std::false_type;
                          using type = Default;
                      };
                  
                      template <class Default, template<class...> class Op, class... Args>
                      struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
                      {
                          using value_t = std::true_type;
                          using type = Op<Args...>;
                      };
                  
                  } // namespace detail
                  
                  // special type to indicate detection failure
                  struct nonesuch {
                      nonesuch() = delete;
                      ~nonesuch() = delete;
                      nonesuch(nonesuch const&) = delete;
                      void operator=(nonesuch const&) = delete;
                  };
                  
                  template <template<class...> class Op, class... Args>
                  using is_detected =
                      typename detail::detector<nonesuch, void, Op, Args...>::value_t;
                  
                  template <template<class...> class Op, class... Args>
                  using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;
                  
                  template <class Default, template<class...> class Op, class... Args>
                  using detected_or = detail::detector<Default, void, Op, Args...>;
                  

                  然后可以簡單地實現檢測方法存在的特征:

                  Traits to detect presence of method can then be simply implemented:

                  template <typename T, typename ...Ts>
                  using foo_type = decltype(std::declval<T>().foo(std::declval<Ts>()...));
                  
                  struct C1 {};
                  
                  struct C2 {
                      int foo(char) const;
                  };
                  
                  template <typename T>
                  using has_foo_char = is_detected<foo_type, T, char>;
                  
                  static_assert(!has_foo_char<C1>::value, "Unexpected");
                  static_assert(has_foo_char<C2>::value, "Unexpected");
                  
                  static_assert(std::is_same<int, detected_t<foo_type, C2, char>>::value,
                                "Unexpected");
                  
                  static_assert(std::is_same<void, // Default
                                             detected_or<void, foo_type, C1, char>>::value,
                                "Unexpected");
                  static_assert(std::is_same<int, detected_or<void, foo_type, C2, char>>::value,
                                "Unexpected");
                  

                  這篇關于如何在檢測習語中要求精確的函數簽名?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 不能)
                      • <bdo id='gCVYD'></bdo><ul id='gCVYD'></ul>
                          <tbody id='gCVYD'></tbody>

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

                          <legend id='gCVYD'><style id='gCVYD'><dir id='gCVYD'><q id='gCVYD'></q></dir></style></legend>

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

                            <tfoot id='gCVYD'></tfoot>

                            主站蜘蛛池模板: 国产精品a久久久久 | 黄色大片在线 | 亚洲欧美中文日韩在线v日本 | 日韩av在线免费 | 色伊人 | 国产精品一区二区在线播放 | 成人午夜在线观看 | 免费一级片 | 亚洲一区视频在线 | 久久久tv | 免费视频99 | 久久精品一级 | 成人免费观看男女羞羞视频 | www.久久99 | www免费视频 | 黄色网毛片 | 亚洲不卡av在线 | 性在线 | 成人av在线网站 | av影音资源| 99久久婷婷国产综合精品首页 | 日本久久久一区二区三区 | 手机三级电影 | 亚洲国产精品一区二区久久 | 91麻豆精品国产91久久久更新资源速度超快 | 久久9热| 天天干视频 | 日本视频在线 | 日韩黄a | 久久久久国产精品一区二区 | 日韩高清黄色 | 国产美女久久 | 免费观看的av毛片的网站 | 亚洲精品福利在线 | 日韩欧美在线视频 | 久久精品国产免费看久久精品 | 国产精品中文 | 日韩三级免费网站 | 日韩高清一区二区 | 伊人狼人影院 | 国产第一亚洲 |