Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wrap BOOST in a separate namespace?

I'm looking to have two versions of BOOST compiled into a project at the same time. Ideally they should be usable along these lines:

boost_1_36_0::boost::shared_ptr<SomeClass> someClass = new SomeClass();
boost_1_35_0::boost::regex expression("[0-9]", boost_1_35_0::boost::regex_constants::basic);
like image 238
Eclipse Avatar asked Sep 23 '08 18:09

Eclipse


People also ask

Are all Boost libraries header-only?

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. The only Boost libraries that must be built separately are: Boost.

Is Boost format header-only?

"Boost, but only the header files." This repo is meant to provide a lightweight, minimal Boost distribution to satisfy SciPy's limited set of Boost dependencies. No binaries are features relying on compiled libraries are available through this header-only package.


2 Answers

I read (well scanned) through the development list discussion. There's no easy solution. To sum up:

  1. Wrapping header files in a namespace declaration

    namespace boost_1_36_0 {
        #include <boost_1_36_0/boost/regex.hpp>
    }
    namespace boost_1_35_0 {
        #include <boost_1_35_0/boost/shared_ptr.hpp>
    }
    
    • Requires modifying source files
    • Doesn't allow for both versions to be included in the same translation unit, due to the fact that macros do not respect namespaces.
  2. Defining boost before including headers

    #define boost boost_1_36_0
        #include <boost_1_36_0/boost/regex.hpp>
    #undef boost
    #define boost boost_1_35_0
        #include <boost_1_35_0/boost/shared_ptr.hpp>
    #undef boost
    
    • Source files can simply be compiled with -Dboost=boost_1_36_0
    • Still doesn't address macro conflicts in a single translation unit.
    • Some internal header file inclusions may be messed up, since this sort of thing does happen.

      #if defined(SOME_CONDITION)
      #  define HEADER <boost/some/header.hpp>
      #else
      #  define HEADER <boost/some/other/header.hpp>
      #endif
      

      But it may be easy enough to work around those cases.

  3. Modifying the entire boost library to replace namespace boost {..} with namespace boost_1_36_0 {...} and then providing a namespace alias. Replace all BOOST_XYZ macros and their uses with BOOST_1_36_0_XYZ macros.
    • This would likely work if you were willing to put into the effort.
like image 118
3 revs Avatar answered Sep 17 '22 21:09

3 revs


Using bcp can install boost library to a specific location and can replace all 'namespace boost' in their code to a custom alias. Assuming our alias is 'boost_1_36_0' all 'namespace boost' code blocks will start with 'boost_1_36_0'. Something like

bcp --namespace=boost_1_36_0 --namespace-alias shared_ptr regex /path/to/install

, but check the documentation in the link yourself because I'm not sure if it is legal syntaxis.

like image 38
0xC0DEGURU Avatar answered Sep 21 '22 21:09

0xC0DEGURU