Search code examples
c++boost-variant

Boost variant with custom type failing when using custom visitor (derived from boost::static_visitor)


I am experimenting with boost::variant and printing with standard types like the below, works.

typedef boost::variant<int, std::string> myVariant;

myVariant my;
my = "Hello";
std::cout << my << endl;

But when I introduced custom type

struct MyStructTag
{
   uint32_t ver;
   double kind;
   std::string type;
};

typedef boost::variant<MyStructTag, std::string> myVariant;

It threw the following error but couldn't understand it completely.

main.cpp: In instantiation of ‘std::ostream& test_variant::custom_ostream::operator()(const T&) const [with T = test_variant::MyStructTag; std::ostream = std::basic_ostream<char>]’:
v_1_55_0/boost/variant/variant.hpp:938:32:   required from ‘boost::detail::variant::invoke_visitor<Visitor>::result_type boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&, int) [with T = const test_variant::MyStructTag; Visitor = const test_variant::custom_ostream; boost::detail::variant::invoke_visitor<Visitor>::result_type = std::basic_ostream<char>&]’
v_1_55_0/boost/variant/detail/visitation_impl.hpp:113:9:   required from ‘typename Visitor::result_type boost::detail::variant::visitation_impl_invoke_impl(int, Visitor&, VoidPtrCV, T*, mpl_::true_) [with Visitor = boost::detail::variant::invoke_visitor<const test_variant::custom_ostream>; VoidPtrCV = const void*; T = test_variant::MyStructTag; typename Visitor::result_type = std::basic_ostream<char>&; mpl_::true_ = mpl_::bool_<true>]’
v_1_55_0/boost/variant/detail/visitation_impl.hpp:156:9:   required from ‘typename Visitor::result_type boost::detail::variant::visitation_impl_invoke(int, Visitor&, VoidPtrCV, T*, NoBackupFlag, int) [with Visitor = boost::detail::variant::invoke_visitor<const test_variant::custom_ostream>; VoidPtrCV = const void*; T = test_variant::MyStructTag; NoBackupFlag = boost::variant<test_variant::MyStructTag, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::has_fallback_type_; typename Visitor::result_type = std::basic_ostream<char>&]’
v_1_55_0/boost/variant/detail/visitation_impl.hpp:237:5:   required from ‘typename Visitor::result_type boost::detail::variant::visitation_impl(int, int, Visitor&, VoidPtrCV, mpl_::false_, NoBackupFlag, Which*, step0*) [with Which = mpl_::int_<0>; step0 = boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, test_variant::MyStructTag, boost::mpl::l_item<mpl_::long_<1l>, std::basic_string<char>, boost::mpl::l_end> > >, boost::mpl::l_iter<boost::mpl::l_end> >; Visitor = boost::detail::variant::invoke_visitor<const test_variant::custom_ostream>; VoidPtrCV = const void*; NoBackupFlag = boost::variant<test_variant::MyStructTag, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::has_fallback_type_; typename Visitor::result_type = std::basic_ostream<char>&; mpl_::false_ = mpl_::bool_<false>]’
v_1_55_0/boost/variant/variant.hpp:2237:13:   required from ‘static typename Visitor::result_type boost::variant<T0, TN>::internal_apply_visitor_impl(int, int, Visitor&, VoidPtrCV) [with Visitor = boost::detail::variant::invoke_visitor<const test_variant::custom_ostream>; VoidPtrCV = const void*; T0_ = test_variant::MyStructTag; TN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; typename Visitor::result_type = std::basic_ostream<char>&]’
v_1_55_0/boost/variant/variant.hpp:2259:13:   required from ‘typename Visitor::result_type boost::variant<T0, TN>::internal_apply_visitor(Visitor&) const [with Visitor = boost::detail::variant::invoke_visitor<const test_variant::custom_ostream>; T0_ = test_variant::MyStructTag; TN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; typename Visitor::result_type = std::basic_ostream<char>&]’
v_1_55_0/boost/variant/variant.hpp:2281:52:   required from ‘typename Visitor::result_type boost::variant<T0, TN>::apply_visitor(Visitor&) const [with Visitor = const test_variant::custom_ostream; T0_ = test_variant::MyStructTag; TN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; typename Visitor::result_type = std::basic_ostream<char>&]’
v_1_55_0/boost/variant/detail/apply_visitor_unary.hpp:74:43:   required from ‘typename Visitor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = test_variant::custom_ostream; Visitable = const boost::variant<test_variant::MyStructTag, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >; typename Visitor::result_type = std::basic_ostream<char>&]’
main.cpp:34:59:   required from here
main.cpp:23:21: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
          return os_ << val;
                     ^
In file included from /usr/include/c++/4.8.2/iostream:39:0,
                 from main.cpp:1:
/usr/include/c++/4.8.2/ostream:602:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = test_variant::MyStructTag]’
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^
In file included from v_1_55_0/boost/variant/variant.hpp:2325:0,
                 from v_1_55_0/boost/variant.hpp:17,
                 from main.cpp:2:
v_1_55_0/boost/variant/detail/variant_io.hpp: In instantiation of ‘void boost::detail::variant::printer<OStream>::operator()(const T&) const [with T = test_variant::MyStructTag; OStream = std::basic_ostream<char>]’:
v_1_55_0/boost/variant/variant.hpp:938:32:   required from ‘boost::detail::variant::invoke_visitor<Visitor>::result_type boost::detail::variant::invoke_visitor<Visitor>::internal_visit(T&, int) [with T = const test_variant::MyStructTag; Visitor = boost::detail::variant::printer<std::basic_ostream<char> >; boost::detail::variant::invoke_visitor<Visitor>::result_type = void]’
v_1_55_0/boost/variant/detail/visitation_impl.hpp:113:9:   required from ‘typename Visitor::result_type boost::detail::variant::visitation_impl_invoke_impl(int, Visitor&, VoidPtrCV, T*, mpl_::true_) [with Visitor = boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char> > >; VoidPtrCV = const void*; T = test_variant::MyStructTag; typename Visitor::result_type = void; mpl_::true_ = mpl_::bool_<true>]’
v_1_55_0/boost/variant/detail/visitation_impl.hpp:156:9:   required from ‘typename Visitor::result_type boost::detail::variant::visitation_impl_invoke(int, Visitor&, VoidPtrCV, T*, NoBackupFlag, int) [with Visitor = boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char> > >; VoidPtrCV = const void*; T = test_variant::MyStructTag; NoBackupFlag = boost::variant<test_variant::MyStructTag, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::has_fallback_type_; typename Visitor::result_type = void]’
v_1_55_0/boost/variant/detail/visitation_impl.hpp:237:5:   required from ‘typename Visitor::result_type boost::detail::variant::visitation_impl(int, int, Visitor&, VoidPtrCV, mpl_::false_, NoBackupFlag, Which*, step0*) [with Which = mpl_::int_<0>; step0 = boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<mpl_::long_<2l>, test_variant::MyStructTag, boost::mpl::l_item<mpl_::long_<1l>, std::basic_string<char>, boost::mpl::l_end> > >, boost::mpl::l_iter<boost::mpl::l_end> >; Visitor = boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char> > >; VoidPtrCV = const void*; NoBackupFlag = boost::variant<test_variant::MyStructTag, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::has_fallback_type_; typename Visitor::result_type = void; mpl_::false_ = mpl_::bool_<false>]’
v_1_55_0/boost/variant/variant.hpp:2237:13:   required from ‘static typename Visitor::result_type boost::variant<T0, TN>::internal_apply_visitor_impl(int, int, Visitor&, VoidPtrCV) [with Visitor = boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char> > >; VoidPtrCV = const void*; T0_ = test_variant::MyStructTag; TN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; typename Visitor::result_type = void]’
v_1_55_0/boost/variant/variant.hpp:2259:13:   required from ‘typename Visitor::result_type boost::variant<T0, TN>::internal_apply_visitor(Visitor&) const [with Visitor = boost::detail::variant::invoke_visitor<boost::detail::variant::printer<std::basic_ostream<char> > >; T0_ = test_variant::MyStructTag; TN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; typename Visitor::result_type = void]’
v_1_55_0/boost/variant/variant.hpp:2281:52:   required from ‘typename Visitor::result_type boost::variant<T0, TN>::apply_visitor(Visitor&) const [with Visitor = boost::detail::variant::printer<std::basic_ostream<char> >; T0_ = test_variant::MyStructTag; TN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}; typename Visitor::result_type = void]’
v_1_55_0/boost/variant/detail/variant_io.hpp:88:5:   required from ‘std::basic_ostream<_CharT, _Traits>& boost::operator<<(std::basic_ostream<_CharT, _Traits>&, const boost::variant<U0, UN ...>&) [with E = char; T = std::char_traits<char>; U0 = test_variant::MyStructTag; UN = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]’
main.cpp:47:17:   required from here
v_1_55_0/boost/variant/detail/variant_io.hpp:64:14: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
         out_ << operand;

Full code

#include <iostream>
#include <boost/variant.hpp>
#include <cstdint>
namespace test_variant {
   struct MyStructTag
   {
     uint32_t ver;
     double kind;
     std::string type;
   };

   typedef boost::variant<MyStructTag, std::string> myVariant;

   struct custom_ostream : public boost::static_visitor<std::ostream&>
   {
      std::ostream& os_;

      custom_ostream(std::ostream& os) : os_(os) {}
#if 1
      template <typename T>
      std::ostream& operator()(T const& val) const
      {
         return os_ << val;
      }
#else
      std::ostream& operator()(MyStructTag const& val) const
      {
         return os_ << val.type;
      }
#endif

      friend std::ostream& operator<<(std::ostream& os, myVariant const& v)
      {
         return boost::apply_visitor(custom_ostream{os}, v);
      }
  };

using namespace test_variant;

int main(int argc, char* agrv[])
{
   myVariant my;

   my = "test";
   std::cout << my << std::endl;

   return 0;
}

Code is compiled with :

g++ --std=c++11 -o main -Iv_1_55_0/ main.cpp

Tried singling out visiting custom boundedtype with no luck.

Any inputs will be helpful.


Solution

  • The reason for the error is that operator<<(std::ostream& os, myVariant const& v) is not found via ADL.

    Argument-dependent lookup is a procedure that involves extending the set of namespaces that are examined in the name lookup for an unqualified function call, that std::cout << my is an example of. That is, you don't spell std::cout.operator<<(my), nor std::operator<<(std::cout, my).

    ADL builds a set of associated namespaces, where the name of the function (here operator<<) can potentially be found. These namespaces could be, e.g.:

    • the namespace of the type of an argument,
    • the namespace of the type of a template argument of the type of an argument.

    The arguments to operator<< are:

    • std::cout, which lives in the std namespace,
    • boost::variant, which lives in the boost namespace.

    In addition to these namespaces, also the namespaces of template arguments are considered, and these are:

    • MyStructTag, which lives in the test_variant namespace,
    • std::string, which lives in the std namespace.

    On top of that, ADL considers associated classes for arguments that are instantiations of class templates, and here these are:

    • std::string,
    • MyStructTag.

    At this point, operator<<(std::ostream& os, myVariant const& v) could be found via ADL, if it were declared inside, e.g., the test_variant namespace itself. However, that function is declared and defined inside custom_ostream, and for friend functions declared in classes, they are not visible to ADL unless that class is an associated class. Since custom_ostream is not in the set of associated classes, the lookup fails to find operator<< overload matching the call arguments.

    In order to fix that, define operator<< in the test_variant namespace itself, and inside custom_ostream just leave a friend declaration alone.

    DEMO