Boost logo

Boost Users :

Subject: [Boost-users] boost::variant and SFINAE
From: Jonathan Chambers` (chamber_j_at_[hidden])
Date: 2009-12-03 08:15:14


I'm using boost::variant in the following manner

 

 

struct Visitor : boost::static_visitor<>

{

    void operator()(int &i) const

    {

        i = 1;

    }

};

 

boost::variant<int, char> var = 0;

 

boost::apply_visitor(Visitor(), 0);

 

 

I'm trying to get the program to compile if the variant takes on the guise of an int (because I've implemented void operator()(int &i) const, but not compile if I make the variant take the guise of a char e.g. boost::variant<int, char> var = 'a'; because I've not implemented the void operator()(char &c) const

 

Can anyone think of a way of doing this, maybe using SFINAE (Substitution Failure Is Not An Error)

 

I'm thinking of doing something along the lines of :

 

template <typename T>

struct SetObj;

 

template <>

struct SetObj<int>

{

     void operator()(int &obj) const

     {

           obj = 1;

     }

}

 

struct Visitor : boost::static_visitor<>

{

     template <typename T>

     void operator()(T& obj) const

     {

          SetObj<T>()(obj);

     }

};

 

But not sure how to take it to the next level to get the desired behaviour.
                                               
_________________________________________________________________
Have more than one Hotmail account? Link them together to easily access both
 http://clk.atdmt.com/UKM/go/186394591/direct/01/



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