<bdo id='3XQWJ'></bdo><ul id='3XQWJ'></ul>
<legend id='3XQWJ'><style id='3XQWJ'><dir id='3XQWJ'><q id='3XQWJ'></q></dir></style></legend>

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

      <small id='3XQWJ'></small><noframes id='3XQWJ'>

        c ++可变参数模板构造函数和通用构造函数

        时间:2023-09-26

        <i id='1S3Xg'><tr id='1S3Xg'><dt id='1S3Xg'><q id='1S3Xg'><span id='1S3Xg'><b id='1S3Xg'><form id='1S3Xg'><ins id='1S3Xg'></ins><ul id='1S3Xg'></ul><sub id='1S3Xg'></sub></form><legend id='1S3Xg'></legend><bdo id='1S3Xg'><pre id='1S3Xg'><center id='1S3Xg'></center></pre></bdo></b><th id='1S3Xg'></th></span></q></dt></tr></i><div id='1S3Xg'><tfoot id='1S3Xg'></tfoot><dl id='1S3Xg'><fieldset id='1S3Xg'></fieldset></dl></div>
          <tbody id='1S3Xg'></tbody>
              • <bdo id='1S3Xg'></bdo><ul id='1S3Xg'></ul>
                <tfoot id='1S3Xg'></tfoot>
                • <small id='1S3Xg'></small><noframes id='1S3Xg'>

                  <legend id='1S3Xg'><style id='1S3Xg'><dir id='1S3Xg'><q id='1S3Xg'></q></dir></style></legend>
                  本文介绍了c ++可变参数模板构造函数和通用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  类似 (c++14) 的代码:

                  Code like (c++14):

                  struct S { int a; int b; };
                  
                  class C
                  {
                    public:
                      C(char const*, size_t) {} // 1
                      C(S const&) {} // 2
                      C(S const*) {} // 3
                      template<typename ...T> C(T&& ...) {} // 4
                  
                   // C(S) {} // 5
                   // C(S*) {} // 6
                  };
                  
                  S s { 1, 2 };
                  C c1 { s }; // calls 4 and not 2
                  C c2 { "abc", 3 }; // calls 4 and not 1
                  C c3 { (char const*)"abc", (size_t)3 }; // calls 1 - ok
                  C c4 { s }; // calls 5 if uncommented
                  C c5 { &s }; // calls 6 if uncommented
                  S const s2 {};
                  C c6 { &s2 }; // calls 3
                  

                  如果简单构造函数与传递的参数具有完全相同的签名,则调用它.是否有一些技巧可以像往常一样将通用构造函数与可变参数模板构造函数一起使用,而不需要复制类、作为参数传递和重载构造函数,例如:

                  Simple constructor is called if it has exact the same signature as the passed parameter. Is there some trick to use common constructors as usual with a variadic template constructor, without copying classes, passed as parameters, and overloading constructors like:

                  C(S const*) {}
                  C(S*) {}
                  

                  并且在构造函数中没有额外的标签

                  And without additional tags in constructors

                  推荐答案

                  创建两层构造函数.然后标记调度.

                  Create two tiers of constructor. Then tag dispatch.

                  template<template<class...>class Z, class T>
                  struct is_template:std::false_type{};
                  template<template<class...>class Z, class...Ts>
                  struct is_template<Z, Z<Ts...>>:std::true_type{};
                  
                  struct foo {
                  private:
                    template<class T> struct tag{ explicit tag(int) {} };
                  public:
                    foo( tag<std::true_type>, const char*, size_t );
                    template<class...Ts>
                    foo( tag<std::false_type>, Ts&&...ts );
                  
                  public:
                    foo() = default; // or whatever
                    template<class T0, class...Ts,
                      std::enable_if_t<!is_template<tag, std::decay_t<T0>>{},int> =0>
                    foo(T0&&t0, Ts&&...ts):
                      foo( tag<typename std::is_constructible< foo, tag<std::true_type>, T0&&, Ts&&... >::type>{0}, std::forward<T0>(t0), std::forward<Ts>(ts)... )
                    {}
                  };
                  

                  首选"ctors 以std::true_type 为前缀,次要"ctors 以std::false_type 为前缀.

                  The "preferred" ctors are prefixed with std::true_type, the "less preferred" ctors are prefixed with std::false_type.

                  这具有完美转发的通常缺陷.例如,如果您采用初始化列表,您将需要另一个明确采用它的公共"构造函数.并且函数名参数神奇的重载不起作用.NULL 是一个 int.等

                  This has the usual imperfections of perfect forwarding. If you take initializer lists, you'll want to have another "public" ctor that takes that explicitly, for example. And function name argument magical overloading won't work. NULL is an int. Etc.

                  您可以想象一个版本,它不是具有两层,而是具有任意数量.is_constructible<... > 面向公众的 ctor 中的子句被替换为一些找到最高 N 的魔法,这样 tag, blah... 可以构造对象(或, 最低的 N,无论你想怎么做).然后它返回类型 tag,然后分派到该层.

                  You can imagine a version that, instead of having two tiers, has an arbitrary number. The is_constructible< ... > clause in the public facing ctor instead is replaced with some magic that finds the highest N such that tag<N>, blah... can construct the object (or, lowest N, whichever way you want to do it). Then it returns the type tag<N>, which then dispatches to that tier.

                  使用这样的技术:

                  template <typename... T,
                        typename = std::enable_if_t<!std::is_constructible<C, T&&...>::value>
                         >
                  C(T&&... ) { }
                  

                  遇到了一个严重的问题,因为我们已经实例化了is_constructible它得到错误答案的上下文.在实践中,编译器会缓存模板实例化的结果,所以现在 is_constructible 的结果取决于编译器顺序(我怀疑违反 ODR).

                  runs into a serious problem down the road, as we have instantiated is_constructible in a context where it gets the answer wrong. And in practice, compilers cache the results of template instantiations, so now the result of is_constructible is compiler order dependent (ODR violation I suspect).

                  这篇关于c ++可变参数模板构造函数和通用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:与硬件接口的 C++ 构造函数应该真正起作用吗? 下一篇:在 new (c++) 的构造函数调用中不使用括号

                  相关文章

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

                      <bdo id='qqPKy'></bdo><ul id='qqPKy'></ul>
                    <legend id='qqPKy'><style id='qqPKy'><dir id='qqPKy'><q id='qqPKy'></q></dir></style></legend>
                    <tfoot id='qqPKy'></tfoot>

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