• <small id='SgmLt'></small><noframes id='SgmLt'>

      <tfoot id='SgmLt'></tfoot>

    1. <legend id='SgmLt'><style id='SgmLt'><dir id='SgmLt'><q id='SgmLt'></q></dir></style></legend>

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

        检查是否在可变参数模板参数包中传递了类型

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

            <legend id='37Oji'><style id='37Oji'><dir id='37Oji'><q id='37Oji'></q></dir></style></legend>
              <bdo id='37Oji'></bdo><ul id='37Oji'></ul>
              <i id='37Oji'><tr id='37Oji'><dt id='37Oji'><q id='37Oji'><span id='37Oji'><b id='37Oji'><form id='37Oji'><ins id='37Oji'></ins><ul id='37Oji'></ul><sub id='37Oji'></sub></form><legend id='37Oji'></legend><bdo id='37Oji'><pre id='37Oji'><center id='37Oji'></center></pre></bdo></b><th id='37Oji'></th></span></q></dt></tr></i><div id='37Oji'><tfoot id='37Oji'></tfoot><dl id='37Oji'><fieldset id='37Oji'></fieldset></dl></div>

              <small id='37Oji'></small><noframes id='37Oji'>

              1. <tfoot id='37Oji'></tfoot>

                  本文介绍了检查是否在可变参数模板参数包中传递了类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在某处听说,使用新的 C++1z 语法,检查类型是否在可变参数模板参数包中传递非常容易 - 显然,您可以使用接近一行的代码来执行此操作.这是真的?这些相关功能是什么?(我尝试查看折叠表达式,但我不知道如何在该问题中使用它们...)

                  I've heard somewhere, that using new C++1z syntax, it is really easy to check if a type is passed in variadic template parameter pack - apparently you can do this with code that is near one-line long. Is this true? What are those relevant features? (I tried looking through fold expressions but I can't see how to use them in that problem...)

                  以下是我在 C++11 中解决问题的方法以供参考:

                  Here's how I solved the problem in C++11 for reference:

                  #include <type_traits>
                  
                  
                  template<typename T, typename ...Ts>
                  struct contains;
                  
                  template<typename T>
                  struct contains<T> {
                      static constexpr bool value = false;
                  };
                  
                  template<typename T1, typename T2, typename ...Ts>
                  struct contains<T1, T2, Ts...> {
                      static constexpr bool value = std::is_same<T1, T2>::value ? true : contains<T1, Ts...>::value;
                  };
                  

                  推荐答案

                  您正在寻找 std::disjunction.它在 N4564 中有规定[元.逻辑].

                  #include <type_traits>
                  
                  template<typename T, typename... Ts>
                  constexpr bool contains()
                  { return std::disjunction_v<std::is_same<T, Ts>...>; }
                  
                  static_assert(    contains<int,      bool, char, int, long>());
                  static_assert(    contains<bool,     bool, char, int, long>());
                  static_assert(    contains<long,     bool, char, int, long>());
                  static_assert(not contains<unsigned, bool, char, int, long>());
                  

                  现场演示

                  或者,适应一个 struct

                  template<typename T, typename... Ts>
                  struct contains : std::disjunction<std::is_same<T, Ts>...>
                  {};
                  

                  <小时>

                  或者,使用折叠表达式


                  Or, using fold expressions

                  template<typename T, typename... Ts>
                  struct contains : std::bool_constant<(std::is_same<T, Ts>{} || ...)>
                  {};
                  

                  现场演示

                  这篇关于检查是否在可变参数模板参数包中传递了类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:函数模板的部分特化 下一篇:如何结合 std::bind()、可变参数模板和完美转发?

                  相关文章

                  最新文章

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

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

                    1. <tfoot id='WIGbD'></tfoot>