So, I see that specialized templated function behaves like non-templated function.
Correct.
Is there any other solution to prevent linker error without static
declaration?
Yes, like any normal non-template function:
Either Define the function specializations as inline
or Declare (but do not define) the specializations in the header:
template<> void TemplatedFunction<float>(float* p);
template<> void TemplatedFunction<char>(char* p);
and define them in Functions.cpp
template<> void TemplatedFunction<float>(float* p) {}
template<> void TemplatedFunction<char>(char* p) {}
These are better options than using static
, because making the functions static has other side effects, such as giving the functions different addresses in each translation unit, and making local static variables different in every translation unit. That's a semantic change, not just a solution to the linker error.
If functions are declared static
, does modern C++ compiler remove them from optimized build, if they are not used?
Yes. But in any files where they are used, you will get duplicate code, making the executable larger than if there was a single definition of the function.
Assuming that I cannot move specialization to .cpp file and leave it in .h file with static
...
I can't see any good reason why that would be necessary, but anyway ...
... or inline
, does this cause code duplication and bloating in optimized build, when these functions are added to every .cpp file where .h file is included, even if they are not used?
No, if they are not used in a given file they will not affect the size of the object compiled from that file.
Your question is misleading because it appears to be about templates (the title is very clearly about templates!) but whether unused inline/static functions cause code bloat is independent of whether the functions are templates or not.
If your question is simply "do unused inline and static functions affect object size?" the answer is no, for both normal functions and function templates.