c++ - 我如何组织 C++ 代码而不用过多考虑某些内容是否已模板化?

标签 c++ templates compilation linker

(我觉得这一定是重复的,但我找不到)。

考虑:

airplane.hpp:

template<class T>
void wings();

void tail();

现在...在哪里定义wings()tail()?我想在同一个地方定义它们;我不想考虑 wings() 是模板化的而 tail() 不是。也许你会明白为什么我有时会写:

airplane.hpp:

template<class T>
void wings();

void tail();

#ifndef airplane_cpp
#define header
#endif
#include "airplane.cpp"

飞机.cpp:

#define airplane_cpp
#include "airplane.hpp"

template<class T>
void wings() { }

#ifndef header
void tail() { }
#endif

但这肯定不是最好的方法。

编辑: 看来我在 TI DSP 芯片上编程可能是相关的,根据 the docsinline 关键字对生成的代码有定义的结果:

The inline keyword specifies that a function is expanded inline at the point at which it is called rather than by using standard calling procedures. The compiler performs inline expansion of functions declared with the inline keyword.

最佳答案

如果将它们设为内联,则可以在 header 中定义所有函数:

template<class T>
inline void wings() {}
// inline not really needed here, but if you don't want to think about it...

inline void tail() {}

关于c++ - 我如何组织 C++ 代码而不用过多考虑某些内容是否已模板化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11320602/

相关文章:

c++ - 值到类型运行时映射

c++ - 协变虚函数和智能指针

在运行时编译常量

java - 使用不同的包编译java项目

c++ - 从 txt 文件中读取以字符分隔的元素

c++ - 从 Qt 应用程序启动外部程序

c++ - 访问静态和非静态的模板构造函数和函数

c++ - C++中的单例多线程代码

c++ - 导出内联声明的类函数

java - 用javac编译java类不起作用