Boost logo

Boost Users :

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


Any ideas on this one?

From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Jonathan Leonard
Sent: Thursday, April 02, 2009 2:13 PM
To: boost-users_at_[hidden]
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



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