Boost logo

Boost Users :

Subject: [Boost-users] How to fwd decl boost::variant (with type sequences)?
From: Paul (peebor_at_[hidden])
Date: 2010-07-17 16:56:57


In a large project (winxp, boost 1.38.0, 10K sources) we are using the
boost::variant at some places. The variant types are visible to a large
amount of cpp’s. Many of these source-file are not ‘using’ the variant
at all, a small amount only ‘passes’ a variant type, and an even smaller
amount actually uses it (copy, static-visitor, etc). We have noticed
however that the compile-time increases for all sources-files, even when
the variant is not used. On a single source-file its maybe split of a
second extra but with 10K sources it quickly becomes problem.

First attempt to solve this is to fwd declare the variant using provided
header variant_fwd.hpp.

Example:

[v.h]

#include <boost/variant/variant_fwd.hpp>

class I001{ public: };
class I002{ public: };
class I003{ public: };

typedef boost::variant<
   boost::shared_ptr<C001>,
   boost::shared_ptr<C002>,
   boost::shared_ptr<C003> > v;

[a.h]

#include “v.h”

class CA
{
public:
   void foo(const v& ref);
};

Source-files now compile faster while only the sources that actually use
the variant need to add #include <boost/variant/variant.hpp> to get the
definition of the variant class.

However it doesn’t really fit our needs because we need the ‘counted’
variant types (using typelists). Reasons for this is simply that some
variants are only 3, 5 or 10 elements while another holds 39 element
types. We don’t want to extend the length of each type to say 50.
Therefore we first compose an mpl::vector (and even compose subsets
using joint_view) before defining the variant types.

Example of use:

#include <boost/mpl/vector.hpp>
#include <boost/variant.hpp>

   typedef boost::mpl::vector<
     boost::shared_ptr<I001>,
     boost::shared_ptr<I002>,
     boost::shared_ptr<I003>
> MplV;
   typedef boost::make_variant_over<MplV>::type v;

Now each cpp file must not only include the variant definitions again
but also (part of) mpl.

How could we solve this issue?

One idea we had is to wrap the variant in a class and fwd decl that.
Although it works I’m not sure if that is the way to go?

Thanks for any advise,
Paul


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net