Hi, I've wrapped a variant<double,bool> into a tuple. Unpacking the tuple works as far I can see. I want to print the tuple for doubles with one pretty print functor and for booleans with another functor (not shown here). Anyway, variant_print takes a long argument list as the error message shows. How can I pass it convinient? I guess, inside the variant_print I have to use the vistor to dispatch the print functors, isn't? BTW, fusion could be used? On debian/linux 1.35 is not released yet, so I use 1.34.1. Thanks, Olaf ---8<--- #include <boost/variant.hpp> #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <iostream> struct pretty_print { pretty_print( double d ) : m_d( d ) { } std::ostream& print_on( std::ostream& os ) const { os << m_d; return os; } double m_d; }; std::ostream& operator<<( std::ostream& os, const pretty_print& p ) { return p.print_on( os ); } struct variant_print { // ????????? variant_print(::boost::variant<double, bool>& v) { } }; using namespace std; using namespace boost::tuples; using namespace boost; int main() { typedef variant<double, bool> double_variant; typedef tuple<double_variant> double_tuple; double_tuple v1; double_tuple v2; get<0>( v1 ) = 3.14; get<0>( v2 ) = true; // This works cout << pretty_print( get<double>( get<0>( v1 ) ) ) << endl; // This not cout << variant_print( get<0>( v1 ) ) << endl; } --->8--- $ LANG=en g++ -Wall tuple.cpp -o tuple && ./tuple tuple.cpp: In function 'int main()': tuple.cpp:55: error: no match for 'operator<<' in 'std::cout << variant_print(((boost::variant<double, bool, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>&)((boost::variant<double, bool, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>*)boost::tuples::get [with int N = 0, HT = boost::variant<double, bool, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, TT = boost::tuples::null_type](((boost::tuples::cons<boost::variant<double, bool, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, boost::tuples::null_type>&)(& v1.boost::tuples::tuple<boost::variant<double, bool, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>::<anonymous>))))))' ...
On Wed, 09 Apr 2008 20:39:26 +0200 Olaf Peter <ope-devel@gmx.de> wrote:
Hi,
I've wrapped a variant<double,bool> into a tuple. Unpacking the tuple works as far I can see. I want to print the tuple for doubles with one pretty print functor and for booleans with another functor (not shown here).
Anyway, variant_print takes a long argument list as the error message shows. How can I pass it convinient?
This is the type of your variant. The first two are double and bool, the rest are placeholders that would be replaced if your variant held more types.
I guess, inside the variant_print I have to use the vistor to dispatch the print functors, isn't?
That's basically how I'd do it. I've modified your example to demonstrate this: ---8<--- #include <boost/variant.hpp> #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <iostream> typedef boost::variant<double, bool> double_variant; typedef boost::tuple<double_variant,double_variant> double_tuple; class output_visitor : public boost::static_visitor<std::ostream&> { std::ostream& os; public: output_visitor(std::ostream& os_):os(os_) {}; result_type operator()(const double &arg) const { os << arg; return os; } result_type operator()(const bool & arg) const { if (arg) { os << "TRUE"; } else { os << "FALSE"; } return os; } }; std::ostream& operator<<( std::ostream& os, const double_variant& p ) { output_visitor out_v(os); boost::apply_visitor(out_v,p); return os; } std::ostream& operator<<( std::ostream& os, const double_tuple& p ) { os << "("<<p.get<0>()<<","<<p.get<1>()<<")"; return os; } int main() { double_tuple v1; double_tuple v2; v1.get<0>() = 3.14; v1.get<1>() = true; v2.get<0>() = false; v2.get<1>() = 2.718; std::cout << v1 << std::endl; std::cout << v2 << std::endl; } // Output is: // (3.14,TRUE) // (FALSE,2.718) --->8--- Jeffrey
Thank you Jeffrey.
typedef boost::variant<double, bool> double_variant; typedef boost::tuple<double_variant,double_variant> double_tuple; ... std::ostream& operator<<( std::ostream& os, const double_variant& p ) { output_visitor out_v(os); boost::apply_visitor(out_v,p); return os; } ...
This works, but is there a generic way without global typedefs? Thanks, Olaf
On Thu, 10 Apr 2008 07:21:08 +0200 Olaf Peter <ope-devel@gmx.de> wrote:
Thank you Jeffrey.
typedef boost::variant<double, bool> double_variant; typedef boost::tuple<double_variant,double_variant> double_tuple; ... std::ostream& operator<<( std::ostream& os, const double_variant& p ) { output_visitor out_v(os); boost::apply_visitor(out_v,p); return os; } ...
This works, but is there a generic way without global typedefs?
You don't have to use the typedefs. Just replace double_variant with boost::variant<double, bool> and double_tuple with boost::tuple<double_variant,double_variant>. Is that what you were asking? Jeffrey
Thank you Jeffrey.
typedef boost::variant<double, bool> double_variant; typedef boost::tuple<double_variant,double_variant> double_tuple; ... std::ostream& operator<<( std::ostream& os, const double_variant& p ) { output_visitor out_v(os); boost::apply_visitor(out_v,p); return os; } ...
This works, but is there a generic way without global typedefs?
You don't have to use the typedefs. Just replace double_variant with boost::variant<double, bool> and double_tuple with boost::tuple<double_variant,double_variant>. Is that what you were asking?
Yep, after reading this, it was so clear ;-) Thanks, Olaf
participants (2)
-
Jeffrey A. Edlund
-
Olaf Peter