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

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

      <bdo id='PdWvZ'></bdo><ul id='PdWvZ'></ul>
    <tfoot id='PdWvZ'></tfoot>
  2. <legend id='PdWvZ'><style id='PdWvZ'><dir id='PdWvZ'><q id='PdWvZ'></q></dir></style></legend>

    1. 如何在 C++ 中声明/定义相互依赖的模板?

      时间:2023-10-17

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

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

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

                  <tbody id='Z32p9'></tbody>
                本文介绍了如何在 C++ 中声明/定义相互依赖的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                通常在 C++ 中,当我需要类之间的相互依赖时,我在头文件中使用前向声明,然后在每个 cpp 文件中包含两个头文件.

                Usually in C++ when I need interdependencies between classes, I use forward declarations in the header files and then include both header files in each cpp file.

                然而,这种方法在使用模板时会中断.因为模板必须完全在头文件中(不包括将代码放入 cpp 并为每个支持的 T 枚举 template class A<T> 的情况 -这并不总是可行的,例如当 T 是一个 lambda 时).

                However this approach breaks when working with templates. Because templates have to be entirely in the header files (not counting the case when I put the code into cpp and enumerate template class A<T>; for each supported T - this is not always feasible, e.g. when T is a lambda).

                那么有没有办法在 C++ 中声明/定义相互依赖的模板?

                So is there a way to declare/define interdependent templates in C++?

                代码示例

                template<typename T> struct B;
                template<typename T> struct A {
                  void RunA(B<T> *pB) {
                    // need to do something to B here
                  }
                };
                
                template<typename T> struct B {
                  void RunB(A<T> *pA) {
                    // need to do something to A here
                  }
                };
                

                如果我开始对 RunA() 中的 B 做一些事情,我想,我会得到一个缺少定义"的错误,因为只有 B 的前向声明是可用的RunA() 编译的时间.

                If I start doing something to B in RunA(), I think, I will get a "missing definition" error because only forward declaration of B is available by the time RunA() is compiled.

                也许有一些技巧可以组织头文件,例如通过将每个头文件拆分为类定义和方法定义文件,然后以某种奇特的方式包含它们.或者也许可以通过第三/第四类完成某些事情.但我无法想象具体如何做到这一点.

                Perhaps there is some trick to organize header files e.g. by splitting each header into class definition and method definition files, and then including them in some fancy way. Or maybe something can be done via a third/fourth class. But I can't imagine how specifically to do this.

                C++11/14/17 没问题(具体来说,它是 MSVC++2017,工具集 v141).

                C++11/14/17 are ok (specifically, it's MSVC++2017, toolset v141).

                推荐答案

                您可以使用细粒度的标题:

                You can utilize fine-grained headers:

                //  A.forward.hpp
                template<typename T> struct A;
                
                //  A.decl.hpp
                #include "A.forward.hpp"
                #include "B.forward.hpp"
                
                template<typename T> struct A
                {
                    void RunA(B<T> *pB);
                };
                
                //  A.impl.hpp
                #include "A.decl.hpp"
                #include "B.hpp"
                
                template<typename T> void A< T >::
                RunA(B<T> *pB)
                {
                    // need to do something to B here
                }
                
                //  A.hpp // this one should be included by code using A
                #include "A.decl.hpp"
                #include "A.impl.hpp"
                
                //  B.forward.hpp
                template<typename T> struct B;
                
                //  B.decl.hpp
                #include "B.forward.hpp"
                #include "A.forward.hpp"
                
                template<typename T> struct B
                {
                    void RunB(A<T> *pA);
                };
                
                //  B.impl.hpp
                #include "B.decl.hpp"
                #include "A.hpp"
                
                template<typename T> void B< T >::
                RunB(A<T> *pA)
                {
                    // need to do something to A here
                }
                
                //  B.hpp // this one should be included by code using B
                #include "B.decl.hpp"
                #include "B.impl.hpp"
                

                显然所有这些头文件也需要某种我在这里省略的头文件保护.重要通知:.impl.hpp 标头被认为是内部的,不应被外部代码使用,而 .forward.hpp, .decl.hpp.hpp 可以在任何地方使用.

                Obviously all of these headers also need some sort of header guards that I omitted here. Important notice: .impl.hpp headers are considered internal and should never be used by the outside code while .forward.hpp, .decl.hpp and .hpp can be used anywhere.

                这种方法确实引入了循环包含依赖,但这不会导致问题:包含头文件产生的代码结构确保代码部分按以下顺序包含:前向声明、类定义、方法定义.

                This approach does introduce circular include dependencies, however this does not lead to problems: code structure produced by inclusion of headers ensures that code parts are included in the following order: forward declarations, class definitions, methods definitions.

                这篇关于如何在 C++ 中声明/定义相互依赖的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:尝试编译 C 扩展模块时缺少 Python.h 下一篇:链接:致命错误 LNK 1104:无法打开文件“LIBCMT.lib"

                相关文章

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

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

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