开发者

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

开发者 https://www.devze.com 2023-02-16 06:12 出处:网络
I have strange problem with two-level variant struct when boost::exception is included. I have following code snippet:

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_s开发者_运维知识库treamable.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 boost/exception/all 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


I don't think it has anything to do with the boost::exception. It's the output stream "operator <<". But I haven't used the variant as you're using it - only one type; I thought you were supposed to have at least 2 types as this is a "union on steroids", but maybe there's something implicit ... I'll revisit the docs.

Is your code inside the boost namespace? I think your output stream operators clash with the one defined for the exception. Try putting your code in your own namespace.

As for the operator not being executed it might still be a problem of picking the wrong one ... try to use your "<<" operator by prefixing it with the namespace and the resolution operator as you do with std::stringstream.

EDIT: As a follow up to your last comment: you can define your operators in your own namespaces, let's say mynamespace and then use your versions explicitly when needed, e.g.

void TestVariant()
{
  using namespace mynamespace;

  ExpressionItem test;
  std::stringstream str;
  str << test;
}

It will work with the above mentioned example, but I'm not sure if that;s the exact situation you're facing ... and I'm not very familiar with spirit

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号