[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Template instantiation in libraries
From: |
Paul Pluzhnikov |
Subject: |
Re: Template instantiation in libraries |
Date: |
03 Dec 2004 07:41:45 -0800 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Artificial Intelligence) |
Garrett Kajmowicz <garrett@garrett.dyndns.biz> writes:
> However, I am unable to figure out how to get the compiler or
> (ideally) my library code to specify that in the binary that if a version
> of the function is available in the library it is being linked against
> that it should not be kept as a weak symbol.
>
> Is there any easy way to do this after-the-fact with binutils?
Since the "undesired" functions each reside in a separate easily
identifiable section, couldn't you e.g.
objcopy --remove-section
.gnu.linkonce.t._ZNSt9basic_iosIcSt11char_traitsIcEED1Ev
on the objects, before linking the application?
> I have found a partial solution. For cases where I want to provide
> certain versions of a template, I define a specialized version of the
> template in the header file, but put the definition in a .cpp file and
> compile it into the library. This works and prevents instantiation when
> compiling. However, it results in my having to have duplicate copies of
> the code kicking around, which is bad engineering and bad for
> maintenance, etc.
Here is another possible approach. Define all template functions
is a separate .cpp file, and let the user control where they are
instantiated. For example, let's assume you have
/// "include/string"
template <typename T> class basic_string {
basic_string(); // body in separate string.cpp
...
};
typedef basic_string<char> string;
The user who simply '#include <string>' and uses only 'std::string',
doesn't have to do anything: his objects will have
'std::basic_string<char>()::basic_string()' undefined, and your
library will provide a definition at link time.
The (presumably rare) user who uses 'basic_string<wchar_t>' will
get unresoved symbols (assuming you do not have basic_string<wchar_t>
compiled into your library). So you tell that user that he has to
#include <string.cpp> into one of his source files that uses the
"non-standard" instances, and you are done.
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.