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 question about 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?
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
Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
In the namespace std are hunderts or thounsands of functions or constants and so on defined. Most of the time you won't bother with that. But if you define a function with the same name and parameters at one point, you hardly will be able to find the mistake. For example there is std::find .
In addition, C also partitions a program's identifiers into four namespaces. Identifiers in one namespace, are also considered different from identifiers in another. The four namespaces are: Tags for a struct/union/enum.
Advantages of namespace In one program, namespace can help define different scopes to provide scope to different identifiers declared within them. By using namespace - the same variable names may be reused in a different program.
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?
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:
file.hpp
namespace MON { // extern: t_ret func(const t_param& pValue); } // << MON
file.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.
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
.
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:
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.
You can declare the functions in the header:
namespace A { void foo(); }
and implement in the .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:
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:
namespace B { template <class T> inline void foo(const T& t) { std::cout << t.name() << "\n"; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With