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

2009/12/3 Jonathan Chambers`
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
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

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
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
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