NO -- never do this!
It is impossible, you'll likely to get accidental crashes.
The only way to get it done correctly is using namespace renaming: i.e. create alternative
boost version placed in different namespace.
Latest version of BCP provides this option. So you will use something like boost_1_43 instead of boost. But it will be quite transparent for you. But you should still be aware
of that you can't use two versions of boost in same cpp file.
Also take a look on this discussion: Creating Library with backward compatible ABI that uses Boost
The liked script renames namespace, defines and includes so you can actually include two
versions of boost like
#include <boost/foo.hpp>
#include <myboost/bar.hpp>
boost::foo f;
myboost::bar b;
Boost BCP does not allow this.
But still you should be careful as some libraries export extern "C" symbols without
boost prefix, boost::thread and boost::regex's C API (regexec, regcomp)
Edit
As example of such issue create following files:
a.cpp:
template<typename Foo>
Foo add(Foo a, Foo b)
{
return a+b;
}
int foo(int x,int y)
{
return add(x,y);
}
b.cpp:
template<typename Foo>
Foo add(Foo a, Foo b)
{
return a-b;
}
int bar(int x,int y)
{
return add(x,y);
}
test.cpp:
#include <iostream>
int foo(int,int);
int bar(int,int);
int main()
{
std::cout<< foo(10,20) <<" " <<bar(10,20) << std::endl;
}
Compile them:
g++ a.cpp b.cpp test.cpp
You would expect:
30 -10
But you'll get
30 30
or
-10 -10
Depending on linking order.
So using two boost versions you may accidentally use symbols from other boost and crash
same as in this program symbol int add<int>(int,int)
is resolved to same symbol even
if it is placed in different compilation units.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…