c++ - avoiding template definition in header file -
this question has answer here:
i have following class defined in header a.h :
class { private: template<int i> void method(); };
is there way me keep implementation of method
in own a.cpp file , usual method implementations ? i'm asking because puting implementation in a.h make interface hard read, since it's private function.
it ever instanciated limited values of "i" if matters
you can following (as it's used practice):
a.hpp
class { private: template<int i> void method(); }; #include "a.tcc"
a.tcc
template<int i> void a::method() { // }
note it's important name implementation file different extension .cpp
actually, because confuse of standard build system environments (unless have complete manual selection of translation unit files).
if want have specialized implementations values of i
, can provide them follows:
// specialization 1 template<> void a::method<1>() { // specialization 1 } template<> void a::method<2>() { // specialization 2 }
Comments
Post a Comment