boost::variant Binary visitation

M

morten

When i compile this code in VC71 or VC80 i get the following errors:
The code is copy/paste from the boost.org tutorial. Please help!

error C2039: 'begin' : is not a member of 'boost::variant<T0_,T1>'
with
[
T0_=int,
T1=std::string
]
Test2.cpp(47): error C2039: 'end' : is not a member of
'boost::variant<T0_,T1>'
with
[
T0_=int,
T1=std::string
]
Test2.cpp(47): error C2039: 'begin' : is not a member of
'boost::variant<T0_,T1>'
with
[
T0_=double,
T1=std::string
]
"

#include "boost/variant.hpp"
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

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

};

int main(int argc, char* argv[])
{
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) );

typedef boost::variant<double, std::string> my_variant;

std::vector< my_variant > seq1;
seq1.push_back("pi is close to ");
seq1.push_back(3.14);

std::list< my_variant > seq2;
seq2.push_back("pi is close to ");
seq2.push_back(3.14);

are_strict_equals visitor;
assert( std::equal(v1.begin(), v1.end(), v2.begin(),
boost::apply_visitor( visitor ) ) );

return 0;
}
 
T

Thomas J. Gritzan

morten said:
When i compile this code in VC71 or VC80 i get the following errors:
The code is copy/paste from the boost.org tutorial. Please help!

error C2039: 'begin' : is not a member of 'boost::variant<T0_,T1>'
[...]

begin and end are not members of boost::variant. Thats what the compiler
says. In std::equal you want to compare seq1 and seq2 instead of v1 and v2.
int main(int argc, char* argv[])
{
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) );

typedef boost::variant<double, std::string> my_variant;

std::vector< my_variant > seq1;
seq1.push_back("pi is close to ");
seq1.push_back(3.14);

std::list< my_variant > seq2;
seq2.push_back("pi is close to ");
seq2.push_back(3.14);

are_strict_equals visitor;
assert( std::equal(v1.begin(), v1.end(), v2.begin(),
boost::apply_visitor( visitor ) ) );

return 0;
}

Looks like the boost.org tutorial is broken :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top