Boost logo

Boost Users :

From: Olaf (ope-devel_at_[hidden])
Date: 2008-02-21 01:10:08


Steven Watanabe schrieb:
> AMDG
>
> ope wrote:
>> Hi,
>>
>> I've the following code:
>>
>> ---8<---
>> #include <boost/variant.hpp>
>>
>> #include <iostream>
>> #include <string>
>>
>> struct available
>> : public boost::static_visitor<bool>
>> {
>> 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
>> {
>> static const std::string NA( "NA" );
>>
>> return lhs != NA; //???
>> }
>> };
>>
>> using namespace boost;
>> using namespace std;
>>
>> int main()
>> {
>> variant< double, std::string > R;
>> variant< double, std::string > L;
>>
>> R = 3.14;
>> L = "NA";
>>
>> cout << R << endl;
>> cout << L << endl;
>>
>> bool r = boost::apply_visitor(available(), R );
>> cout << ( r ? "true" : "false" ) << endl;
>>
>> bool l = boost::apply_visitor(available(), L );
>> cout << ( l ? "true" : "false" ) << endl;
>> }
>>
>> --->8---
>>
>> which does not compile. Anyway, from a data parser I get a double or a
>> "NA" (not available) string. This test shall handle both cases. To
>> process using the double I have to check if it available (than use

otherwise use default, this wasn't clear, sorry.

>> defaults). Unfortunately, the static_visitor<bool> needs two arguments.
>> How can I get the syntax (or simpler) as used in the example
>
> Let me make sure that I understand what you want.
> If the variant contains a double the result should be true.
> If the variant contains the string "NA" the result should be false.

That's correct. If it contains a double the string value is empty.

> The variant cannot contain a string that is not "NA"
>
> struct available
> : public boost::static_visitor<bool>
> {
> bool operator()( const double&) const
> {
> return true;
> }
>
> bool operator()( const string& lhs) const
> {
> assert(lhs == "NA");
>
> return false; //???
> }
> };

May this work? There is always a string - empty or holding "NA".

Thanks,
Olaf


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net