D
dizzy
Hello
When working with template heavy code one will have to face the
inherent "problems" of the inclusion model because of the requirement
to provide the definition of the template at every place where it is
used. This means problems with include separation and problems with
code duplication (the later depends largely on your toolchain but
unless I am missing out on a modern standard compliant and free as in
free beer compiler that does remove template code instantiation
duplicates then this is a problem I have to deal with).
1. Include separation
In order to enable implicit template instantiations we must provide
the definition of the template code at the place of instantiation.
This is usually done by having the template code definition either in
the same header as the template interface or included by it. This
means when you have your own header that uses some template class it
will include the template class header which has the member functions
definitions which require their own includes thus bringing include
dependencies that were of the template code implementation alone.
A solution may be to separate the code from the template definitions
that needs its own includes into some non template code for which we
provide only declarations and the template definitions will call upon
them and have them defined in a separate cpp file that will have those
includes.
Or another solution would be a general approach to template code to
have only the declarations (of functions, for class/structs we put
their definition but not with the function members definitions) in the
header that describes the interface of the template code, then have
the definitions in a say .tcc file. Code that wants implicit
instantiation for those templates can include the tcc file directly
(so this needs an include guard itself similar to normal header files)
or can include only the header file with the declarations. Then in the
cpp file of the template code user it can include the .tcc file and
instantiate the templates using explicit instantiation.
Do you have another solution that you know is being used in practice
and seems to work well for medium/large projects?
2. Template instantiation code duplication
For some reason, my toolchain (g++ 4.x, ld 2.18) seems to create huge
executables in size for template heavy programs. This I think is
because the linker does not remove the duplicate code definitions (it
may remove just the duplicate symbols leaving the code to use space).
So if I use std::string in 100 translation units I will get 100
definitions of std::string constructor.
For this problem I can't come up with a reasonable solution, any idea?
PS: g++'s -frepo argument and functionality does not really help; the
overall project compilation time increases and while -frepo may
eliminate some double instancing of some code it certainly doesn't
eliminate multiple instances of very commonly used code (like
std::string)
When working with template heavy code one will have to face the
inherent "problems" of the inclusion model because of the requirement
to provide the definition of the template at every place where it is
used. This means problems with include separation and problems with
code duplication (the later depends largely on your toolchain but
unless I am missing out on a modern standard compliant and free as in
free beer compiler that does remove template code instantiation
duplicates then this is a problem I have to deal with).
1. Include separation
In order to enable implicit template instantiations we must provide
the definition of the template code at the place of instantiation.
This is usually done by having the template code definition either in
the same header as the template interface or included by it. This
means when you have your own header that uses some template class it
will include the template class header which has the member functions
definitions which require their own includes thus bringing include
dependencies that were of the template code implementation alone.
A solution may be to separate the code from the template definitions
that needs its own includes into some non template code for which we
provide only declarations and the template definitions will call upon
them and have them defined in a separate cpp file that will have those
includes.
Or another solution would be a general approach to template code to
have only the declarations (of functions, for class/structs we put
their definition but not with the function members definitions) in the
header that describes the interface of the template code, then have
the definitions in a say .tcc file. Code that wants implicit
instantiation for those templates can include the tcc file directly
(so this needs an include guard itself similar to normal header files)
or can include only the header file with the declarations. Then in the
cpp file of the template code user it can include the .tcc file and
instantiate the templates using explicit instantiation.
Do you have another solution that you know is being used in practice
and seems to work well for medium/large projects?
2. Template instantiation code duplication
For some reason, my toolchain (g++ 4.x, ld 2.18) seems to create huge
executables in size for template heavy programs. This I think is
because the linker does not remove the duplicate code definitions (it
may remove just the duplicate symbols leaving the code to use space).
So if I use std::string in 100 translation units I will get 100
definitions of std::string constructor.
For this problem I can't come up with a reasonable solution, any idea?
PS: g++'s -frepo argument and functionality does not really help; the
overall project compilation time increases and while -frepo may
eliminate some double instancing of some code it certainly doesn't
eliminate multiple instances of very commonly used code (like
std::string)