Any ideas on this one?

 

From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Jonathan Leonard
Sent: Thursday, April 02, 2009 2:13 PM
To: boost-users@lists.boost.org
Subject: [Boost-users] [mpl] newbie question about if_c et al

 

Hi,

 

I’m trying to make a class which is conditionally synchronized.  What I have is like the following:

 

template<bool SYNCH=true>

class c

{

public:

      void f()

      {

            typename boost::mpl::if_c<SYNCH, ScopedCriticalSection<>,

void*>::type scs(cs); // SCS == RAII wrapper around CS

            // do other stuff

      }

 

private:

      typename boost::mpl::if_c<SYNCH, CriticalSection<>, void*>::type cs;

};

 

This is not entirely optimal though as it requires 3 MOV instructions to  create the scs void* copy when SYNCH is false (and I’d rather not lean on compiler optimizations even if they will help in this case).  [Something like NullType, VoidType, or OptionType (a la F#) would be ideal but I can’t seem to locate one (and I’m not sure the type system would allow such a thing—it doesn’t seem to like a simple ‘void’].

 

Any better idiom or alternative approach anyone can offer for this purpose would be greatly appreciated.

 

Many thanks,

Jonathan