0

My application uses variant as a data bucket to carry data from one object to another. The examples I've seen of using apply_visitor() to extract the bound data have void operator() so apply_visitor(), itself, doesn't return anything.

Can anyone point me to some examples where apply_visitor() returns the extracted value?

1 Answers1

3

There's plenty in the Boost.Variant tutorial.

Here's one from there (technically this is binary visitation, but it's the least amount of code to copy for a complete example):

class are_strict_equals
    : public boost::static_visitor<bool>
{
public:    
    template <typename T, typename U>
    bool operator()( const T &, const U & ) const
    {
        return false; // cannot compare different types
    }

    template <typename T>
    bool operator()( const T & lhs, const T & rhs ) const
    {
        return lhs == rhs;
    }    
};

boost::variant< int, std::string > v1( "hello" );

boost::variant< double, std::string > v2( "hello" );
assert( boost::apply_visitor(are_strict_equals(), v1, v2) );

boost::variant< int, const char * > v3( "hello" );
assert( !boost::apply_visitor(are_strict_equals(), v1, v3) );
Barry
  • 286,269
  • 29
  • 621
  • 977