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

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

        如何在命名空间 std 中转发声明模板类?

        时间:2023-05-23

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

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

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

            1. <legend id='dcvsJ'><style id='dcvsJ'><dir id='dcvsJ'><q id='dcvsJ'></q></dir></style></legend>
              1. <tfoot id='dcvsJ'></tfoot>
                  <tbody id='dcvsJ'></tbody>
                • 本文介绍了如何在命名空间 std 中转发声明模板类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..
                  #ifndef __TEST__
                  #define __TEST__
                  
                  namespace std
                  {
                      template<typename T>
                      class list;
                  }
                  
                  template<typename T>
                  void Pop(std::list<T> * l)
                  {
                      while(!l->empty())
                          l->pop();
                  }
                  
                  #endif
                  

                  并在我的 main.js 中使用了该函数.我收到错误.当然,我知道 std::list 有更多的模板参数(我认为是分配器).但是,这不是重点.我是否必须知道模板类的完整模板声明才能转发声明?

                  and used that function in my main. I get errors. Of course, I know that there are more template params for std::list (allocator I think). But, that is beside the point. Do I have to know the full template declaration of a template class to be able to forward declare it?

                  我之前没有使用指针 - 它是一个引用.我用指针试试.

                  I wasn't using a pointer before - it was a reference. I'll try it out with the pointer.

                  推荐答案

                  问题不在于您不能向前声明模板类.是的,您确实需要知道所有模板参数及其默认值才能正确地向前声明它:

                  The problem is not that you can't forward-declare a template class. Yes, you do need to know all of the template parameters and their defaults to be able to forward-declare it correctly:

                  namespace std {
                    template<class T, class Allocator = std::allocator<T>>
                    class list;
                  }
                  

                  但是标准明确禁止在 namespace std 中进行这样的前向声明:唯一你被允许放入 std 是一个模板特殊化,通常是用户定义类型的std::less.如有必要,其他人可以引用相关文本.

                  But to make even such a forward declaration in namespace std is explicitly prohibited by the standard: the only thing you're allowed to put in std is a template specialisation, commonly std::less on a user-defined type. Someone else can cite the relevant text if necessary.

                  只需#include ,不用担心.

                  哦,顺便说一句,任何包含双下划线的名称都保留供实现使用,因此您应该使用诸如 TEST_H 之类的东西,而不是 __TEST__.它不会生成警告或错误,但如果您的程序与实现定义的标识符发生冲突,则不能保证编译或运行正确:它格式错误.还禁止以下划线开头的名称,后跟大写字母等.一般来说,除非您知道要处理的魔法是什么,否则不要以下划线开头.

                  Oh, incidentally, any name containing double-underscores is reserved for use by the implementation, so you should use something like TEST_H instead of __TEST__. It's not going to generate a warning or an error, but if your program has a clash with an implementation-defined identifier, then it's not guaranteed to compile or run correctly: it's ill-formed. Also prohibited are names beginning with an underscore followed by a capital letter, among others. In general, don't start things with underscores unless you know what magic you're dealing with.

                  这篇关于如何在命名空间 std 中转发声明模板类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 标准实践:虚拟接口类与模板 下一篇:如何 typedef 模板类?

                  相关文章

                  最新文章

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

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

                    2. <small id='C6zK9'></small><noframes id='C6zK9'>