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

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

      如何使用 sfinae 选择构造函数?

      How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
    2. <i id='vp5sJ'><tr id='vp5sJ'><dt id='vp5sJ'><q id='vp5sJ'><span id='vp5sJ'><b id='vp5sJ'><form id='vp5sJ'><ins id='vp5sJ'></ins><ul id='vp5sJ'></ul><sub id='vp5sJ'></sub></form><legend id='vp5sJ'></legend><bdo id='vp5sJ'><pre id='vp5sJ'><center id='vp5sJ'></center></pre></bdo></b><th id='vp5sJ'></th></span></q></dt></tr></i><div id='vp5sJ'><tfoot id='vp5sJ'></tfoot><dl id='vp5sJ'><fieldset id='vp5sJ'></fieldset></dl></div>

          <tbody id='vp5sJ'></tbody>
      1. <small id='vp5sJ'></small><noframes id='vp5sJ'>

          <bdo id='vp5sJ'></bdo><ul id='vp5sJ'></ul>
          • <tfoot id='vp5sJ'></tfoot>

                <legend id='vp5sJ'><style id='vp5sJ'><dir id='vp5sJ'><q id='vp5sJ'></q></dir></style></legend>
                本文介绍了如何使用 sfinae 选择构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在模板元编程中,可以在返回类型上使用SFINAE来选择某个模板成员函数,即

                In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e.

                template<int N> struct A {
                  int sum() const noexcept
                  { return _sum<N-1>(); }
                private:
                  int _data[N];
                  template<int I> typename std::enable_if< I,int>::type _sum() const noexcept
                  { return _sum<I-1>() + _data[I]; }
                  template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept
                  { return _data[I]; }
                };
                

                但是,这不适用于构造函数.假设,我想声明构造函数

                However, this doesn't work on constructors. Suppose, I want to declare the constructor

                template<int N> struct A {
                   /* ... */
                   template<int otherN>
                   explicit(A<otherN> const&); // only sensible if otherN >= N
                };
                

                但不允许 otherN <N.

                那么,可以在这里使用 SFINAE 吗?我只对允许自动模板参数推导的解决方案感兴趣,所以

                So, can SFINAE be used here? I'm only interested in solutions which allow automatic template-parameter deduction, so that

                A<4> a4{};
                A<5> a5{};
                A<6> a6{a4};  // doesn't compile
                A<3> a3{a5};  // compiles and automatically finds the correct constructor
                

                注意:这是一个非常简化的示例,其中 SFINAE 可能有点矫枉过正,而 static_assert 可能就足够了.但是,我想知道我是否可以改用 SFINAE.

                Note: this is a very simplified example where SFINAE may be overkill and static_assert may suffice. However, I want to know whether I can use SFINAE instead.

                推荐答案

                您可以向模板添加默认类型参数:

                You can add a defaulted type argument to the template:

                template <int otherN, typename = typename std::enable_if<otherN >= N>::type>
                explicit A(A<otherN> const &);
                

                这篇关于如何使用 sfinae 选择构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                Why is the copy-constructor argument const?(为什么复制构造函数参数是常量?)

                  <legend id='gcvpP'><style id='gcvpP'><dir id='gcvpP'><q id='gcvpP'></q></dir></style></legend>
                    <tbody id='gcvpP'></tbody>

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

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