如何仅为每个功能构建一个没有内联陈述的标题lib?
在我的代码中,我具有标准和模板的功能。在我看来,在foo.cpp中的定义以及foo.h.中的模板函数的定义,在foo.h中声明标准函数并不是一个好习惯。在两个文件中都有定义有点混乱。像这样:
foo.h
//Declaration of standard function
void stdFunc(int i);
//Definition of template function
template<typename T>
inline void templFunc(const T& t){
cout << t << endl;
}
foo.cpp ,
#include "foo.h"
//Definition of standard function
void stdFunc(int i){
cout << i << endl;
}
所以我决定在标题中定义我的功能。但是现在,所有函数都需要具有内联陈述以避免链接器错误,这也不建议。
boost 或 cgal 不将所有功能声明为内联?
In my code, I have standard and templatized functions. It seems to me that it is not good practice to have the declaration of standard functions in foo.h, their definition in foo.cpp, and the definition of template functions in foo.h. It's a little messy to have definition in both files. Like that :
foo.h
//Declaration of standard function
void stdFunc(int i);
//Definition of template function
template<typename T>
inline void templFunc(const T& t){
cout << t << endl;
}
foo.cpp
#include "foo.h"
//Definition of standard function
void stdFunc(int i){
cout << i << endl;
}
So I decided to define my functions in the headers. But now, all functions need to have the inline statment to avoid linker error, which is not recommended either.
How do header-only libraries like Boost or CGAL to not declare all their functions as inline ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要一个仅限标题库,则必须只包含模板或内联函数。没有其他方法。
注意:内联与内线无关,但意味着该函数可能在翻译单元中多次出现,而无需在链接时给出重复的定义错误。
可以承诺所有函数的动力都是相同的,并且链接器应将其中一个选为代表对象。
If you want a header-only library then it must only contain templates or inline functions. There is no other way.
Note: inline has nothing to do with inlining but means the function may appear multiple times in the translation units without giving a duplicate defintion error when linking.
It's a promise that all the ocurances of the functions will be the same and the linker shall pick one of them as the representing object.