Assume that we have define a template function in a X.cpp source file as below:
/............. X.cpp .........................../
template<class Container>
static inline void foo(const Container& container,std::string& content)
{
/.../
}
static
and inline
here?cpp
file,will this affect to reduce build time for each translation unit?Do we gain any compiler/linker optimization using static and inline here?
Not likely. The static
qualifier makes the template function have internal linkage. Meaning that the name foo
will refer to it only inside the translation unit of X.cpp
. If that very same definition appears in Y.cpp
, it's a different template function for all intents and purposes.
The inline
specifier is probably a misguided attempt to encourage the compiler into inlining the call. Since compilers do it even without the specifier, and can even ignore it if they choose to, it's pointless. The most one can say is that it clarifies intent. But YMMV on how good an idea it is.
If we have considerable amount of template functions definitions in a cpp file,will this affect to reduce build time for each translation unit?
Don't try to do the compiler/linker's work for them. Specify the linkage that makes sense. Put the definition in a header/cpp file based on the same rationale, and let your build system work.
If you artificially start making templates static and including them in different translation units, your compiler/linker is very likely going to err on the side of caution and blow up your executable size.