boost::exception 和 boost::variant 组合的问题

发布于 2024-10-22 02:08:26 字数 1602 浏览 1 评论 0原文

当包含 boost::exception 时,我对两级变体结构有奇怪的问题。我有以下代码片段:

#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; }

当我尝试编译它时,出现以下错误:

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>   ]

代码片段已尽可能简化,在实际代码中结构要复杂得多,每个变体都有五个子类型。

当我删除 #include boost/exception/all 并尝试以下测试片段时,程序已正确编译:

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

有人可以告诉我如何定义运算符 <<即使使用 boost::Exception 也能正常工作?

谢谢并问候

瑞克

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-10-29 02:08:26

我认为这与 boost::Exception 没有任何关系。它是输出流“operator <<”。但我没有像你一样使用它的变体——只有一种类型;我认为你应该至少有两种类型,因为这是一个“类固醇联盟”,但也许有一些隐含的东西......我会重新审视文档。

您的代码位于 boost 命名空间内吗?我认为您的输出流运算符与为异常定义的运算符冲突。尝试将代码放入您自己的命名空间中。

至于操作符没有被执行,它可能仍然是一个选择错误的问题......尝试使用你的“<<”运算符,方法是在其前面加上命名空间和解析运算符,就像使用 std::stringstream 一样。

编辑: 作为上一条评论的后续内容:您可以定义运算符在您自己的命名空间中,假设 mynamespace ,然后在需要时显式使用您的版本,例如

void TestVariant()
{
  using namespace mynamespace;

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

它将适用于上述示例,但我不确定这是否是您的确切情况面对......而我对精神不太熟悉

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文