boost::variant and SFINAE

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/

2009/12/3 Jonathan Chambers` <chamber_j@hotmail.com>:
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 don't think SFINAE will help here, since you don't have a template. What about just declaring, but not defining, the version with a char parameter? Though since it's a runtime distinction, I'm not convinced this will be possible at compile-time, since it will want to compile the code for all the runtime possibilities. What's wrong with just throwing an exception in the char-parameter function in the visitor? Or, alternatively, why are you using a variant<char, int> if you never want it to be a char?

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
boost::variant and boost::static_visitor involve *static* (i.e. compile-time) type checking, so it can't matter what actual type you feed into the variant in *runtime*.

Jonathan Chambers` wrote:
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
The value (and thus actual type) of a variant is a runtime property; there cannot be a way to achieve what you want. Sebastian
participants (4)
-
Igor R
-
Jonathan Chambers`
-
Scott McMurray
-
Sebastian Redl