函数对象内部的内部调用(Boost::apply_visitor 特定)

发布于 2024-10-07 10:25:36 字数 2667 浏览 0 评论 0原文

我目前正在编写一个集合表达式求值器,它生成 set< T>并设置<设置< T> >,下面的代码是显示表达式的结果。

class string_visitor : public boost::static_visitor<string>
{
public:
    string operator()(bool value) const
    {
        return "{}";
    }

    string operator()(set<T> value) const
    {
        set<T>::const_iterator it = value.begin();
        string output = "{";

        if(!value.empty())
        {
            output += *it; // Return an empty set if necessary.
            ++it;
        }

        for(; it != value.end(); ++it)
        {
            output += " " + *it;
        }
        output += "}";

        return output;
    }

    string operator()(set<set<T> > value) const
    {
        set<set<T> >::const_iterator it = value.begin();
        string output = "{";

        if(!value.empty())
        {
            output += boost::apply_visitor(string_visitor(), *it); // Return an empty set if necessary.
            ++it;
        }

        for(; it != value.end(); ++it)
        {
            output += " " + boost::apply_visitor(string_visitor(), *it);
        }
        output += "}";

        return output;
    }
};

当我尝试使用集合代码评估集合集合时,我遇到的问题发生了,显然我正在使用它,因为这是很好的做法,但编译器似乎不喜欢我用来构造调用的语法。

output += boost::apply_visitor(string_visitor(), *it);

有两条这样的线,它们产生痕迹..

e:\documents\level 3\advanced software Engineering\coursework\coursework\boost\variant\detail\apply_visitor_unary.hpp(76) : 错误 C2039: 'apply_visitor' : 不是 'std::set

的成员_Kty>' 1>和 1> [ 1> _Kty=std::字符串 1> ] 1> e:\documents\level 3\advanced software Engineering\coursework\coursework\context.h(96) :参见函数模板实例化 'std::basic_string<_Elem,_Traits,_Ax> 的参考; boost::apply_visitor::ExpressionTree::string_visitor,const std::set<_Kty>>(const Visitor &,Visitable &)' 正在编译 1>和 1> [ 1> _Elem=字符, 1> _Traits=std::char_traits, 1> _Ax=std::分配器, 1> T=std::字符串, 1> _Kty=std::字符串, 1>访客=上下文::ExpressionTree::string_visitor, 1>可访问=const std::set 1> ] 1> e:\documents\level 3\advanced software Engineering\coursework\coursework\context.h(90) : 编译类模板成员函数 'std::string Context::ExpressionTree::string_visitor::operator ()(std::设置<_Kty>) const' 1>和 1> [ 1> T=std::字符串, 1> _Kty=std::设置 1> ] 1> e:\documents\level 3\advanced software Engineering\coursework\coursework\context.cpp(337) :请参阅正在编译的类模板实例化 'Context::ExpressionTree::string_visitor' 的引用 1>和 1> [ 1> T=std::字符串 1> ]

有谁知道如何表达这种呼叫吗?

干杯, 亚历克斯

I am currently writing a set expression evaluator which generates set< T > and set< set< T > >, the code below is to display the result of the expression.

class string_visitor : public boost::static_visitor<string>
{
public:
    string operator()(bool value) const
    {
        return "{}";
    }

    string operator()(set<T> value) const
    {
        set<T>::const_iterator it = value.begin();
        string output = "{";

        if(!value.empty())
        {
            output += *it; // Return an empty set if necessary.
            ++it;
        }

        for(; it != value.end(); ++it)
        {
            output += " " + *it;
        }
        output += "}";

        return output;
    }

    string operator()(set<set<T> > value) const
    {
        set<set<T> >::const_iterator it = value.begin();
        string output = "{";

        if(!value.empty())
        {
            output += boost::apply_visitor(string_visitor(), *it); // Return an empty set if necessary.
            ++it;
        }

        for(; it != value.end(); ++it)
        {
            output += " " + boost::apply_visitor(string_visitor(), *it);
        }
        output += "}";

        return output;
    }
};

The problem I am experiencing is happening when I try to evaluate sets of sets using the set code, obviously I am using this as it is good practice but the compiler doesn't appear to like the syntax I am using to construct the call.

output += boost::apply_visitor(string_visitor(), *it);

There are two lines like that, they produce the trace..

e:\documents\level 3\advanced software engineering\coursework\coursework\boost\variant\detail\apply_visitor_unary.hpp(76) : error C2039: 'apply_visitor' : is not a member of 'std::set<_Kty>'
1> with
1> [
1> _Kty=std::string
1> ]
1> e:\documents\level 3\advanced software engineering\coursework\coursework\context.h(96) : see reference to function template instantiation 'std::basic_string<_Elem,_Traits,_Ax> boost::apply_visitor::ExpressionTree::string_visitor,const std::set<_Kty>>(const Visitor &,Visitable &)' being compiled
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits,
1> _Ax=std::allocator,
1> T=std::string,
1> _Kty=std::string,
1> Visitor=Context::ExpressionTree::string_visitor,
1> Visitable=const std::set
1> ]
1> e:\documents\level 3\advanced software engineering\coursework\coursework\context.h(90) : while compiling class template member function 'std::string Context::ExpressionTree::string_visitor::operator ()(std::set<_Kty>) const'
1> with
1> [
1> T=std::string,
1> _Kty=std::set
1> ]
1> e:\documents\level 3\advanced software engineering\coursework\coursework\context.cpp(337) : see reference to class template instantiation 'Context::ExpressionTree::string_visitor' being compiled
1> with
1> [
1> T=std::string
1> ]

Does anyone have any idea of how to phrase that sort of call?

Cheers,
Alex

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

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

发布评论

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

评论(1

昇り龍 2024-10-14 10:25:36

通过简单地将变量类型转换回我定义的变体类型来解决问题。所以就我而言,解决方法是:

output += boost::apply_visitor(string_visitor(), (Context<T>::ExpressionTree::stackType) *it);

希望对其他人有帮助!

Managed to fix the problem by simply type-casting the variable back to the type of variant I defined. so in my case it was solved by:

output += boost::apply_visitor(string_visitor(), (Context<T>::ExpressionTree::stackType) *it);

Hope that helps out others!

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