C++ 命名空间函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10492847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Namespace Functions
提问by K Hein
I am a Java developer and I am pretty new to C++. I need to implement some kind of utility class, and I was thinking of implementing the methods as static. However, I came across to this stackoverflow questionabout namespace functions vs static method, and apprently namespace functions is preferred approach. So I would like to konw if there is any article or example on how to implement namespace function. E.g, how should I declare namespace functions in header file? Should header only contain function definitions like class header file and implementations should be in cpp file, or should I straight away implement functions in header file?
我是一名 Java 开发人员,我对 C++ 还很陌生。我需要实现某种实用程序类,并且我正在考虑将这些方法实现为静态方法。但是,我遇到了有关命名空间函数与静态方法的stackoverflow 问题,显然命名空间函数是首选方法。所以我想知道是否有任何关于如何实现命名空间功能的文章或示例。例如,我应该如何在头文件中声明命名空间函数?header 应该只包含函数定义,如类头文件,实现应该在 cpp 文件中,还是应该直接在头文件中实现函数?
Basically, I am trying to implement an application to parse a text file that contains some commands. So I am thinking of implementing static helper methods to handle text processing. E.g readCommand(string line). Please let me know if I am in the wrong direction. Thanks
基本上,我正在尝试实现一个应用程序来解析包含一些命令的文本文件。所以我正在考虑实现静态辅助方法来处理文本处理。例如 readCommand(string line)。如果我走错了方向,请告诉我。谢谢
采纳答案by justin
how should I declare namespace functions in header file?
我应该如何在头文件中声明命名空间函数?
namespace MON {
// extern:
t_ret func(const t_param& pValue);
// 'inline':
inline t_ret inline_func(const t_param& pValue) { ... }
} // << MON
Should header only contain function definitions like class header file and implementations should be in cpp file, or should I straight away implement functions in header file?
header 应该只包含函数定义,如类头文件,实现应该在 cpp 文件中,还是应该直接在头文件中实现函数?
that depends on whether you want them (potentially) inlined or exported. this often comes down to minimizing dependencies.
这取决于您是希望它们(可能)内联还是导出。这通常归结为最小化依赖性。
to expand on exporting or inlining:
扩展导出或内联:
you'd often favor an extern function to minimize dependencies in c++. this is equivalent to separating the definition from the declaration in a class method:
您通常喜欢使用 extern 函数来最小化 C++ 中的依赖项。这相当于将定义与类方法中的声明分开:
file.hpp
文件.hpp
namespace MON {
// extern:
t_ret func(const t_param& pValue);
} // << MON
file.cpp
文件.cpp
#include "hefty_stuff.hpp"
MON::t_ret MON::func(const t_param& pValue) { ... }
however, it's at times critical for the definition to be visible in some cases, often for performance or when you know size is important and the header is not included many places. thus, the inline
variant is also an option.
然而,在某些情况下,定义可见有时很重要,通常是为了性能,或者当您知道大小很重要并且标题没有包含在很多地方时。因此,inline
变体也是一种选择。
an inline function may still be exported, and it may be inlined as requested -- however, any inline function copies may be merged (specifically, the implementation is free to assume all definitions are equal and any copies of the function are unnecessary).
内联函数仍然可以导出,并且可以根据要求进行内联——但是,任何内联函数副本都可以合并(具体来说,实现可以自由地假设所有定义都相同,并且不需要函数的任何副本)。
with exported definitions, you may selectively restrict (or quarantine) your include dependencies. that is, #include "hefty_stuff.hpp"
need not be in the header to use the functions in file.hpp
.
使用导出的定义,您可以有选择地限制(或隔离)您的包含依赖项。也就是说,#include "hefty_stuff.hpp"
不需要在标题中使用file.hpp
.
Basically, I am trying to implement an application to parse a text file that contains some commands. So I am thinking of implementing static helper methods to handle text processing.
基本上,我正在尝试实现一个应用程序来解析包含一些命令的文本文件。所以我正在考虑实现静态辅助方法来处理文本处理。
well, static
should be avoided here. c++ uses the one-definition-rule. static
will just result in a lot of unnecessary copies. furthermore, an anonymous namespace is the c++ approach to c's static
function:
好吧,static
应该避免在这里。C++ 使用一个定义规则。static
只会导致很多不必要的副本。此外,匿名命名空间是 cstatic
函数的 c++ 方法:
namespace {
t_ret func(const t_param& pValue) { ... }
} // << anon
note: anonymous namespaces may also result in unnecessary copies. the reason you would use them as a substitute for a static function is if you want or need to deviate from the one-definition-rule, and do not want to declare the symbol in a scope which may be 'resolved'.
注意:匿名命名空间也可能导致不必要的副本。您将它们用作静态函数的替代品的原因是,如果您想要或需要偏离单一定义规则,并且不想在可能“已解决”的范围内声明符号。
the final point regards template<>
declarations. with templates, the definition must be visible where used, unless your compiler supports extern templates. for templates, you can accomplish definition visibility in multiple ways. typically, people will simply declare the definition in place, or add a header for the definitions which is included either at the end of the header or as needed. with templates, functions do not need to be declared inline
to avoid multiple definition errors.
最后一点是关于template<>
声明。对于模板,定义必须在使用时可见,除非您的编译器支持 extern 模板。对于模板,您可以通过多种方式实现定义可见性。通常,人们会简单地声明定义,或者为定义添加一个标题,该标题包含在标题的末尾或根据需要。使用模板,函数不需要声明inline
以避免多个定义错误。
回答by juanchopanza
You can declare the functions in the header:
您可以在标题中声明函数:
namespace A {
void foo();
}
and implement in the .cpp:
并在 .cpp 中实现:
namespace A {
void foo() { std::cout << "foo!"; }
}
You can also put the implementation in the header, making sure to declare it inline
to avoid breaking the one definition rule:
您还可以将实现放在标题中,确保声明它inline
以避免破坏一个定义规则:
namespace A {
inline void foo() { std::cout << "foo()!"; }
}
Note that putting the implementation in the header means that client code has a compilation dependency on the implementation, as well as on headers used for the implementation. In the example above, the client code now depends on the the header, and if we do something trivial like add an exclamation mark to the printout, we need to re-compile, as opposed to re-link, all the client code.
请注意,将实现放在标头中意味着客户端代码对实现以及用于实现的标头具有编译依赖性。在上面的例子中,客户端代码现在依赖于头,如果我们做一些微不足道的事情,比如在打印输出中添加一个感叹号,我们需要重新编译,而不是重新链接,所有的客户端代码。
It is very important to put implementations of template functions in the header or in a file included by the header, these cannot go in the .cpp:
将模板函数的实现放在头文件或头文件包含的文件中非常重要,这些不能放在 .cpp 中:
namespace B {
template <class T>
inline void foo(const T& t) { std::cout << t.name() << "\n"; }
}
回答by Monkey Shen
how should I declare namespace functions in header file?
我应该如何在头文件中声明命名空间函数?
namespace YourNamespace
{
void fun1();
void fun2();
}
Should header only contain function definitions like class header file and implementations should be in cpp file, or should I straight away implement functions in header file?
header 应该只包含函数定义,如类头文件,实现应该在 cpp 文件中,还是应该直接在头文件中实现函数?
If your functions in the namespace are static, you can implement functions in header file, or you must implement in cpp file.
如果命名空间中的函数是静态的,可以在头文件中实现,也可以在cpp文件中实现。