Using boost with explicit template instance
We have built a C++ library using Boost and when we try to integrate the library in a binary we are having problem in linking because of “--instances=explicit” option used for building the binary.
The problem is, when we use --instances=explicit option (of Sun CC compiler) it expects us to instantiate the templates explicitly. As Boost internally uses templates when we use Boost with this option the linker is throwing “Undefined symbol” errors - which is expected as we are not instantiating the templates used in Boost explicitly.
Though I understand why the linker is throwing the error here, it looks tricky to instantiate all boost templates explicitly in the code. So, want to check if there any solution/workaround to use Boost with “-instances=explicit” option of Sun CC compiler.
Any help is much appreciated.
We can reproduce the problem with simple sample code as -
#include<iostream>
#include
On Oct 10, 2011, at 10:24 PM, ranadheer p
We have built a C++ library using Boost and when we try to integrate the library in a binary we are having problem in linking because of “--instances=explicit” option used for building the binary.
There's an old joke in which a patient complains to his doctor: "Doctor, it hurts when I move my arm like this." The doctor replies succinctly: "Then don't move your arm like that." May I ask why you want to enforce explicit template instantiation? Much of Boost consists of metaprogramming, "template magic," in which the compiler is able to perform some pretty amazing feats because of implicit template instantiation. If you were required to research all the internal templates implicitly used by your code and hand-code correct instantiations, I assume it would be easier to hand-code all your functionality in the first place, avoiding Boost. I've never even heard of that switch; I fail to appreciate what you would gain by using it. My advice would be to turn it off and happily use Boost, saving yourself months (if not years) of reinventing equivalent functionality.
The only advantage of using explicit template instantiation is that it seems like it would result in least compilation time and small object sizes (http://download.oracle.com/docs/cd/E19205-01/819-5267/bkagp/index.html). As a note, I don't know how good this is from relying on compiler to instantiate. As I said, I also understand that it doesn't make sense to instantiate all internal boost templates explicitly but we are planning to use this library in a legacy binary which is using "--instances=explicit" option. So, just wanted to confirm before I remove "--instances=explicit" and resolve the other issues which might be resulting of this removal. -- View this message in context: http://boost.2283326.n4.nabble.com/Using-boost-with-explicit-template-instan... Sent from the Boost - Users mailing list archive at Nabble.com.
On Tue, Oct 11, 2011 at 8:13 AM, Ranadheer
The only advantage of using explicit template instantiation is that it seems like it would result in least compilation time and small object sizes (http://download.oracle.com/docs/cd/E19205-01/819-5267/bkagp/index.html). As a note, I don't know how good this is from relying on compiler to instantiate.
As I said, I also understand that it doesn't make sense to instantiate all internal boost templates explicitly but we are planning to use this library in a legacy binary which is using "--instances=explicit" option. So, just wanted to confirm before I remove "--instances=explicit" and resolve the other issues which might be resulting of this removal.
The two justifiable reasons for explicit instantiation I have encountered in the past 15+ years of C++ development are: 1. a library uses templates in its implementation and needs to provide instantiations that the compiler wouldn't implicitly determine but that, as a provider of the library, you know will be required by code linking the library 2. the implicit instantiation engine sucks Fortunately, #2 isn't often the case any more. And #1 doesn't really justify demanding that the compiler ONLY do explicit instantiation, because it's entirely okay (in my experience) to explicitly instantiate templates as one would do in #1 and still let the compiler do implicit instantiation for the general case.
participants (4)
-
Chris Cleeland
-
Nat Goodspeed
-
Ranadheer
-
ranadheer p