Featured post
c++ - Multiple-File Template Implementation -
with normal functions, declaration , definition separated across multiple files so:
// foo.h namespace foo { void bar(); }
.
// foo.cpp #include "foo.h" void foo::bar() { cout << "inside function." << endl; }
it understanding cannot done templates. declaration , definition must not separate because appropriate form of template created "on-demand" when needed.
so, how , templates typically defined in multiple-file project this? intuition in foo.cpp
because that's "meat" of functions is, on other hand it's header file that's going included.
template code stays in .hh
file. there's no reason make them separate files, though practice put definitions after all declarations.
when compiler generates template code, flags linker knows instantiation of template in 1 compilation unit (i.e. .o
file) exact same code 1 in unit. keep 1 , discard rest, rather bailing out "multiply defined symbol" error.
this true of modern compilers template support. in case of gcc, since 2.8. (i know, wrote lot of code gcc 2.7.2.2 before smartened linker , had jump through hoops make templates build right.)
- Get link
- X
- Other Apps
Comments
Post a Comment