Boost logo

Boost Users :

Subject: Re: [Boost-users] operator<< for std::vector< boost::variant< type_a, type_b >>
From: Chris Cleeland (chris.cleeland_at_[hidden])
Date: 2018-06-18 21:56:38


On Mon, Jun 18, 2018 at 4:23 PM Maarten Verhage via Boost-users <
boost-users_at_[hidden]> wrote:

>
>
> >> In particular, your typedefs are confusing. Are you sure you didn't
> mean
> >> this? It looks like you did based on the rest of the code.
> >>
> >> typedef boost::variant< int, double > parameter_t;
> >> typedef std::vector< parameter_t > employee_t;
>
> I'm actually pursuing this tree-like hierarchy. I might even need to make
> it
> recursive. But I want to start easy.
>
> typedef std::vector< boost::variant< int, double >> parameter_t;
> typedef std::vector< boost::variant< int, double, parameter_t >>
> employee_t;
>

Strange structure, but okay.

>
> Have you looked into the stackoverflow answer by: Richard Hodges? The
> poster
> had the same compiler error and the solution Richard presented is
> significantly more complicated than a single operator<< overload.
>

Yes I did. He tells you the answer--that boost::variant's auto-io visit
looks for an ADL-found operator<< for the types,
and, as your compiler tells you, it can't find one for std::vector<blah>.
Because it's ADL-found, the operators you
defined at global scope will never match. They will only be found if they
are defined in namespace std or in whatever
namespace contains the compile-time invocation of operator<<. As the
stackoverflow answer indicates, neither is
a really great idea, because it's a violation of the standard to introduce
anything into namespace std, and introducing
it into the invoking namespace requires knowing the invoking
namespace--which is an implementation detail of
boost::variant and may change or may even vary depending on the types used.

For grins I used your original and put two operator<< inside `namespace
std`--one for each of your typedefs.
The compiler found them both and created an executable that ran.

Good luck.

-- 
Chris Cleeland


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net