[ref][variant] use together with polymorphic behaviour

Hello! In order to obtain a polymorphic behavior with boost::variant and keep a reference semantic in corresponding visitor I've added the following constructor to boost::reference_wrapper class: template<typename T1> reference_wrapper(reference_wrapper<T1> const& rw): t_(rw.get_pointer()) {} it can be tested with: int i = 1; boost::reference_wrapper<double> = boost::ref(i); I ask someone to add this functionality to boost::reference_wrapper Best, Oleg Abrosimov. This is the code I've tried to make workable (feel free to skip it): #include <iostream> #include <boost/variant.hpp> class base { public : virtual void print() const { std::cout << "base\n"; } }; class child : public base { public : void print() const { std::cout << "child\n"; } }; class my_visitor : public boost::static_visitor<> { public: void operator()(base& b) const { b.print(); } void operator()(int i) const { std::cout << "int\n"; } }; #include <boost/ref.hpp> int main() { child c; typedef boost::variant<boost::reference_wrapper<base>, int> var_t; var_t v = boost::ref(c); boost::apply_visitor( my_visitor(), v ); //prints "child" base b; v = boost::ref(b); boost::apply_visitor( my_visitor(), v ); //prints "base" std::cin.get(); return 0; }

Oleg Abrosimov wrote:
Hello!
In order to obtain a polymorphic behavior with boost::variant and keep a reference semantic in corresponding visitor I've added the following constructor to boost::reference_wrapper class:
template<typename T1> reference_wrapper(reference_wrapper<T1> const& rw): t_(rw.get_pointer()) {}
This makes sense, but...
it can be tested with:
int i = 1; boost::reference_wrapper<double> = boost::ref(i);
... this doesn't; why should a reference to int be convertible to a reference to double?

Peter Dimov ?????:
Oleg Abrosimov wrote:
Hello!
In order to obtain a polymorphic behavior with boost::variant and keep a reference semantic in corresponding visitor I've added the following constructor to boost::reference_wrapper class:
template<typename T1> reference_wrapper(reference_wrapper<T1> const& rw): t_(rw.get_pointer()) {}
This makes sense, but...
it can be tested with:
int i = 1; boost::reference_wrapper<double> = boost::ref(i);
... this doesn't; why should a reference to int be convertible to a reference to double?
my fault, sorry. it should be class base {}; class child : public base {}; child c; boost::reference_wrapper<base> bref = boost::ref(c);
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Oleg Abrosimov wrote:
my fault, sorry. it should be
class base {}; class child : public base {};
child c; boost::reference_wrapper<base> bref = boost::ref(c);
This compiles if I make the reference_wrapper constructor implicit. I wonder why we made it explicit (in TR1 as well). Probably out of habit. On the other hand, I can't think of any potential problems that an implicit constructor could have caused. Thoughts?
participants (2)
-
Oleg Abrosimov
-
Peter Dimov