Problem with combination boost::exception and boost::variant

Posted by Rick on Stack Overflow See other posts from Stack Overflow or by Rick
Published on 2011-03-12T23:33:50Z Indexed on 2011/03/13 0:10 UTC
Read the original article Hit count: 341

Filed under:
|
|
|

Hello all,

I have strange problem with two-level variant struct when boost::exception is included. I have following code snippet:

#include <boost/variant.hpp>
#include <boost/exception/all.hpp>

typedef boost::variant< int >   StoredValue;
typedef boost::variant< StoredValue > ExpressionItem;
inline std::ostream& operator << ( std::ostream & os, const StoredValue& stvalue ) {    return os;}
inline std::ostream& operator << ( std::ostream & os, const ExpressionItem& stvalue ) { return os; }

When I try to compile it, I have following error:

boost/exception/detail/is_output_streamable.hpp(45): error C2593: 'operator <<' is ambiguous
test.cpp(11): could be 'std::ostream &operator <<(std::ostream &,const ExpressionItem &)' [found using argument-dependent lookup]
test.cpp(8): or       'std::ostream &operator <<(std::ostream &,const StoredValue &)' [found using argument-dependent lookup]
1>  while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, const boost::error_info<Tag,T>)'
1>  with
1>  [
1>    _Elem=char,
1>    _Traits=std::char_traits<char>
1>   ]
1>   and
1>   [
1>       Tag=boost::tag_original_exception_type,
1>       T=const type_info *
1>   ]

Code snippet is simplified as much as possible, in the real code are structures much more complicated and each variant has five sub-types.

When i remove #include and try following test snippet, program is compiled correctly:

void TestVariant()
{
  ExpressionItem test;
  std::stringstream str;
  str << test;
}

Could someone please advise me how to define operators << in order to function even when using boost::Exception ?

Thanks and regards

Rick

© Stack Overflow or respective owner

Related posts about c++

Related posts about exception