• <tfoot id='nPdPn'></tfoot>
  • <small id='nPdPn'></small><noframes id='nPdPn'>

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

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

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

        SFINAE 尝试使用 bool 给出编译器错误:“模板参数‘T::value’涉及模板参数";

        时间:2023-10-18
        <tfoot id='IEMJG'></tfoot>
      1. <i id='IEMJG'><tr id='IEMJG'><dt id='IEMJG'><q id='IEMJG'><span id='IEMJG'><b id='IEMJG'><form id='IEMJG'><ins id='IEMJG'></ins><ul id='IEMJG'></ul><sub id='IEMJG'></sub></form><legend id='IEMJG'></legend><bdo id='IEMJG'><pre id='IEMJG'><center id='IEMJG'></center></pre></bdo></b><th id='IEMJG'></th></span></q></dt></tr></i><div id='IEMJG'><tfoot id='IEMJG'></tfoot><dl id='IEMJG'><fieldset id='IEMJG'></fieldset></dl></div>

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

                <tbody id='IEMJG'></tbody>
              <legend id='IEMJG'><style id='IEMJG'><dir id='IEMJG'><q id='IEMJG'></q></dir></style></legend>
                • <bdo id='IEMJG'></bdo><ul id='IEMJG'></ul>
                  本文介绍了SFINAE 尝试使用 bool 给出编译器错误:“模板参数‘T::value’涉及模板参数";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试使用 bool(不同于流行的 void_ 技巧):

                   模板结构解析{静态常量布尔值 = 假;};模板struct Resolve{静态常量布尔值 = 真;};

                  目标是专门化,其中定义了 static const bool my_value = true; 的类.如果它们已定义 false 或未定义,则不要专门化它.即

                  struct B1 {//专门针对这种情况进行解析static const bool my_value = true;};struct B2 {//不特化static const bool my_value = false;};结构 B3 {};//不专业

                  当在 B1 上应用上述技巧时,它给出了编译错误:

                  解析::value;

                  <块引用>

                  错误:模板参数‘T::my_value’涉及模板参数

                  我知道这可以通过其他方式实现.但是,我很想知道,为什么它会在此处给出编译器错误,并且可以在此代码本身中解决吗?

                  解决方案

                  实际上你在做什么是第 §14.5.4/9 节禁止的,它说,

                  <块引用>

                  部分特化的非类型参数表达式不应涉及部分特化的模板参数,除非参数表达式是一个简单的标识符.

                  技巧也可以使用 type 作为第二个模板参数,封装 非类型 值,如下所述:

                  template结构布尔类型{};template>结构解析{静态常量布尔值 = 假;};模板struct Resolve>{静态常量布尔值 = 真;};

                  现在它编译罚款.

                  I tried to implement an SFINAE using bool (unlike popular void_ trick):

                    template<typename T, bool = true>
                    struct Resolve
                    {
                      static const bool value = false;
                    };
                  
                    template<typename T>
                    struct Resolve<T, T::my_value>
                    {
                      static const bool value = true;
                    };
                  

                  The goal is to specialize, the classes which have static const bool my_value = true; defined inside it. If they are defined false or not defined then don't specialize it. i.e.

                  struct B1 {  // specialize Resolve for this case
                    static const bool my_value = true;
                  };
                  struct B2 {  // don't specialize
                    static const bool my_value = false;
                  };
                  struct B3 {};  // don't specialize
                  

                  When applying the above trick on B1 it gives the compilation error:

                  Resolve<B1>::value;
                  

                  error: template argument ‘T::my_value’ involves template parameter(s)

                  I am aware that this can be achieved with alternate ways. However, I am interested in knowing, why it gives compiler error here and can it be solved in this code itself ?

                  解决方案

                  Actually what you're doing is forbidden by section §14.5.4/9 which says,

                  A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier.

                  The trick could be using a type for second template parameter as well, encapsulating the non-type value, as described below:

                  template<bool b> struct booltype {};
                  
                  template<typename T, typename B = booltype<true> >
                  struct Resolve
                  {
                    static const bool value = false;
                  };
                  
                  template<typename T>
                  struct Resolve<T, booltype<T::my_value> >
                  {
                    static const bool value = true;
                  };
                  

                  Now it compile fines.

                  这篇关于SFINAE 尝试使用 bool 给出编译器错误:“模板参数‘T::value’涉及模板参数";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:重载函数的调用不明确 下一篇:“警告:“__const_coal"部分已弃用"在 Mac OS 上将 Xcode 更新到最新版本后

                  相关文章

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

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

                    <bdo id='zXxIk'></bdo><ul id='zXxIk'></ul>

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

                      <tfoot id='zXxIk'></tfoot>