Boost logo

Boost Users :

Subject: [Boost-users] [mpl] newbie question about if_c et al
From: Jonathan Leonard (v-jleonard_at_[hidden])
Date: 2009-04-02 17:12:49


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



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