Boost logo

Boost Users :

Subject: Re: [Boost-users] operator<< for std::vector< boost::variant< type_a, type_b >>
From: Maarten Verhage (m_r_verhage_at_[hidden])
Date: 2018-06-18 22:46:59



----- Original Message -----
From: Chris Cleeland via Boost-users<mailto:boost-users_at_[hidden]>
To: boost-users<mailto:boost-users_at_[hidden]>
Cc: Chris Cleeland<mailto:chris.cleeland_at_[hidden]>
Sent: Monday, June 18, 2018 23:56
Subject: Re: [Boost-users] operator<< for std::vector< boost::variant< type_a, type_b >>



On Mon, Jun 18, 2018 at 4:23 PM Maarten Verhage via Boost-users <boost-users_at_[hidden]<mailto: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

________________________________

Thanks Chris,

Make if '<<' isn't so suitable. I can better make it work with the visitor and not using the << operator to sent it to ostream.

Regards, Maarten



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