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