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

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

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

        哪个编译器是对的?需要模板化返回类型之前的“

        时间:2023-10-06

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

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

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

                • <legend id='Hvspu'><style id='Hvspu'><dir id='Hvspu'><q id='Hvspu'></q></dir></style></legend>
                  本文介绍了哪个编译器是对的?需要模板化返回类型之前的“模板"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  此代码段(摘自 this question) 用 g++ 编译得很好(如所见),只要返回类型之前的 template 就在那里.相比之下,VC10 不会编译该代码并出现以下错误:

                  This snippet (taken from this question) compiles fine with g++ (as seen), so long the template before the return type is there. In contrast, VC10 does not compile that code with the following error:

                  错误 C2244:'A::getAttr':无法将函数定义与现有声明相匹配

                  error C2244: 'A::getAttr' : unable to match function definition to an existing declaration

                  如果我删除 template,VC10 很高兴,但 g++ 会发出这个错误:

                  If I remove the template, VC10 is happy but g++ screams this error:

                  错误:用作模板的非模板AttributeType"
                  注意:使用'A::template AttributeType'表示是模板

                  error: non-template 'AttributeType' used as template
                  note: use 'A::template AttributeType' to indicate that it is a template

                  是不是又是因为VC的两阶段查找坏了还是什么原因?哪个编译器就在这里?我怀疑 g++ 是正确的,因为我对这里需要 template 有一个模糊的记忆,就像分配器内部的 rebind 模板一样.

                  Is it again because of VC's broken two-phase look-up or what is the cause? Which compiler is right here? I suspect g++ to be correct, as I have a vague memory of template being needed here, like with the rebind template inside of allocators.

                  编辑:我们有一个赢家:g++/GCC(惊喜...).

                  Edit: We have a winner: g++/GCC (surprise surprise...).

                  template <typename T, typename K>
                  class A {
                  public:
                      T t;
                      K k;
                  
                      template <int i, int unused = 0>
                      struct AttributeType{
                      };
                  
                      template <int i>
                      AttributeType<i> getAttr();
                  
                  };
                  
                  template <typename T, typename K>
                  template <int i>
                  typename A<T, K>::template AttributeType<i> A<T, K>::getAttr() {
                  //                ^^^^^^^^ -- needed or not?
                      return t;
                  }
                  
                  
                  int main(){
                      A<int,int> a;
                  }
                  

                  推荐答案

                  GCC 是对的.AttributeType 是一个依赖的模板名称,后面跟着尖括号<,所以这里需要关键字template来消除歧义1,让编译器清楚后面跟着的是模板名.该规则在 §14.2/4 中提到:

                  GCC is right. AttributeType is a dependent template-name which is followed by angle bracket <, so the keyword template is required here to remove the ambiguity1, making it clear to the compiler that what is followed is a template-name. The rule is mentioned in §14.2/4:

                  当成员模板名称特化出现在 之后.或 ->在后缀表达式中,或之后嵌套名称说明符合格 ID,以及后缀表达式或限定 ID明确依赖于模板参数(14.6.2),成员模板名称必须加前缀通过关键字模板.否则假设名称命名为非模板.

                  When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

                  1 @Johannes 在这里写了一个很好的解释:

                  1 @Johannes has written a very good explanation here:

                  我必须将模板"放在哪里以及为什么要放?和类型名称"关键字?

                  这篇关于哪个编译器是对的?需要模板化返回类型之前的“模板"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 中的固定宽度整数 下一篇:在调试模式下找不到 msvcr90d.dll

                  相关文章

                  最新文章

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

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

                  • <bdo id='O5mws'></bdo><ul id='O5mws'></ul>
                  <legend id='O5mws'><style id='O5mws'><dir id='O5mws'><q id='O5mws'></q></dir></style></legend>