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

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

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

      1. 模板:模板函数不能很好地与类的模板成员函数配

        时间:2023-05-24
          <tbody id='1ZOH1'></tbody>

        <tfoot id='1ZOH1'></tfoot>
        • <bdo id='1ZOH1'></bdo><ul id='1ZOH1'></ul>

            <small id='1ZOH1'></small><noframes id='1ZOH1'>

                • <legend id='1ZOH1'><style id='1ZOH1'><dir id='1ZOH1'><q id='1ZOH1'></q></dir></style></legend>
                  <i id='1ZOH1'><tr id='1ZOH1'><dt id='1ZOH1'><q id='1ZOH1'><span id='1ZOH1'><b id='1ZOH1'><form id='1ZOH1'><ins id='1ZOH1'></ins><ul id='1ZOH1'></ul><sub id='1ZOH1'></sub></form><legend id='1ZOH1'></legend><bdo id='1ZOH1'><pre id='1ZOH1'><center id='1ZOH1'></center></pre></bdo></b><th id='1ZOH1'></th></span></q></dt></tr></i><div id='1ZOH1'><tfoot id='1ZOH1'></tfoot><dl id='1ZOH1'><fieldset id='1ZOH1'></fieldset></dl></div>
                  本文介绍了模板:模板函数不能很好地与类的模板成员函数配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  这是我实际拥有的一些代码的最小测试用例.当它尝试评估 a.getResult() 时失败:

                  This is a minimal test case of some code that I actually have. It fails when it tries to evaluate a.getResult<B>():

                  test.cpp: In function 'void printStuff(const A&)':
                  test.cpp:6: error: expected primary-expression before '>' token
                  test.cpp:6: error: expected primary-expression before ')' token
                  

                  代码是:

                  #include <iostream>
                  
                  template< class A, class B>
                  void printStuff( const A& a)
                  {
                      size_t value = a.getResult<B>();
                      std::cout << value << std::endl;
                  }
                  
                  struct Firstclass {
                      template< class X >
                      size_t getResult() const {
                          X someInstance;
                          return sizeof(someInstance);
                      }
                  };
                  
                  int main(int, char**) {
                      Firstclass foo;
                  
                      printStuff<Firstclass, short int>(foo);
                      printStuff<Firstclass, double>(foo);
                  
                      std::cout << foo.getResult< double >() << std::endl;
                  
                      return 0;
                  }
                  

                  如果我注释掉 printStuff 函数及其调用位置,foo.getResult<double >() 调用编译良好,并符合预期.

                  If I comment out the printStuff function and where it's called, the foo.getResult< double >() call compiles fine and does what is expected.

                  知道发生了什么吗?一段时间以来,我一直在使用广泛的模板化代码,但从未遇到过这样的事情.

                  Any idea what's going on? I've been working with extensively templated code for a while and have never encountered anything like this.

                  推荐答案

                  当您引用属于依赖类型成员的模板时,您必须在它前面加上关键字 template.这就是对 printStuff 内的 getResult 的调用应该是什么样子

                  When you refer to a template that is a member of dependent type, you have to prepend it with a keyword template. This is how the call to getResult inside printStuff should look

                  size_t value = a.template getResult<B>();
                  

                  这类似于在引用依赖类型中的嵌套类型名时使用关键字 typename.出于某种原因,关于typename 嵌套类型的部分是众所周知的,但是对于template 嵌套模板的类似要求却相对未知.

                  This is similar to using the keyword typename when referring to nested typenames in a dependent type. For some reason, the bit about typename with nested types is rather well-known, but the similar requirement for template with nested templates is relatively unknown.

                  请注意,一般的语法结构有点不同.typename 总是放在类型的全名前面,而 ​​template 插入在中间.

                  Note that the general syntax structure is a bit different though. The typename is always put in front of the full name of the type, while template is inserted in the middle.

                  同样,这仅在您访问依赖类型的模板成员时才需要,在上面的示例中为 printStuff 中的 A代码>.当您在 main 中调用 foo.getResult<> 时,foo 的类型是不相关的,因此不需要包含 模板关键字.

                  Again, this is only necessary when you are accessing a template member of a dependent type, which in the above example would be A in printStuff. When you call foo.getResult<> in main the type of foo is not dependent, so there's no need to include the template keyword.

                  这篇关于模板:模板函数不能很好地与类的模板成员函数配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                • <legend id='aJOtT'><style id='aJOtT'><dir id='aJOtT'><q id='aJOtT'></q></dir></style></legend>

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

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