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

        如何在检测习语中要求精确的函数签名?

        时间:2023-05-24
              <tbody id='gUIRz'></tbody>

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

            • <legend id='gUIRz'><style id='gUIRz'><dir id='gUIRz'><q id='gUIRz'></q></dir></style></legend>

                • <bdo id='gUIRz'></bdo><ul id='gUIRz'></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模板网!

                      • <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 id='gCVYD'><tfoot id='gCVYD'></tfoot><dl id='gCVYD'><fieldset id='gCVYD'></fieldset></dl></div>

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