开发者

Internal call inside a function object, (Boost::apply_visitor specific)

开发者 https://www.devze.com 2023-01-30 04:41 出处:网络
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.

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 开发者_StackOverflow中文版+= 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


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!

0

精彩评论

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