[variant] ideas needed

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 defaults). Unfortunately, the static_visitor<bool> needs two arguments. How can I get the syntax (or simpler) as used in the example? Thanks Olaf

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 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. 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; //??? } }; In Christ, Steven Watanabe

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

AMDG Olaf wrote:
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".
I don't quite understand. variant<double, string> holds either a double or a string at any one time, never both. If the variant holds a double then the overload of operator() for double will be called, if a string then the overload for string will be called instead. In Christ, Steven Watanabe

Hi Steven, the problem is related to the static_visitor, e.g. (from docs): boost::variant< int, std::string > u; ... class my_visitor : public boost::static_visitor<int> { public: int operator()(int i) const {return i;} int operator()(const std::string & str) const {return str.length(); } }; same for static_visitor<> etc. BUT static_visitor<bool>: class are_strict_equals : public boost::static_visitor<bool> { public: template <typename T, typename U> bool operator()( const T &, const U & ) const { return false; } template <typename T> bool operator()( const T & lhs, const T & rhs ) const { return lhs == rhs; } }; is binary! I've got compilation errors due to. Thanks, Olaf

AMDG Olaf Peter wrote:
Hi Steven,
the problem is related to the static_visitor, e.g. (from docs):
boost::variant< int, std::string > u; ...
class my_visitor : public boost::static_visitor<int> { public: int operator()(int i) const {return i;}
int operator()(const std::string & str) const {return str.length(); } };
same for static_visitor<> etc. BUT static_visitor<bool>:
<snip>
is binary! I've got compilation errors due to.
apply_visitor can handle two arguments: apply_visitor(are_strict_equals(), v1, v2) In Christ, Steven Watanabe
participants (4)
-
Olaf
-
Olaf Peter
-
ope
-
Steven Watanabe