Suppose I have:
class TypeA { };
class TypeB { };
typedef boost::variant<TypeA, TypeB> Type;
This is ok:
void foo(Type t) { }; int main(){ TypeA a; foo(a); }
This does not compile:
void foo(Type &t) { }; int main(){ TypeA a; foo(a); }
with the error:
invalid initialization of reference of type ‘Type&’ from expression of type ‘TypeA’
Also this does not compile:
void foo(Type *t) { }; int main(){ TypeA a; foo(&a); }
with the error:
cannot convert ‘TypeA*’ to ‘Type*’ for argument ‘1’ to ‘void foo(Type*)’
Is there a way to pass to a function that accepts a boost::variant an instance of one of the types aggregated by that boost::variant, either through a reference (as in case 2) or a pointer (as in case 3)?
Thank you very much!
What really happens in 1:
What cannot happen in 2 or 3:
You have two solutions (for a non template foo):
Type
, then take a pointer/reference to thisboost::variant<TypeA*,TypeB*>
for implicit conversion to kick inA third solution is to change foo itself, and make it template. It depends on what you want to do.