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

  • <small id='0GX40'></small><noframes id='0GX40'>

        <bdo id='0GX40'></bdo><ul id='0GX40'></ul>
    1. <legend id='0GX40'><style id='0GX40'><dir id='0GX40'><q id='0GX40'></q></dir></style></legend>

        <tfoot id='0GX40'></tfoot>

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

        Which compiler is right? #39;template#39; before templated return type needed?(哪个编译器是对的?需要模板化返回类型之前的“模板?)
        1. <tfoot id='8sT4G'></tfoot>
            <bdo id='8sT4G'></bdo><ul id='8sT4G'></ul>
            <legend id='8sT4G'><style id='8sT4G'><dir id='8sT4G'><q id='8sT4G'></q></dir></style></legend>

              <small id='8sT4G'></small><noframes id='8sT4G'>

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

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

                  问题描述

                  此代码段(摘自 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:

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

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

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

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

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

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

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