templates - C++ boost::variant generic converter -


I've been trying for the last three days to find out how boost :: variant & lt; ... & gt;, but it's been quite difficult.

The one that I can solve here is that it is not normal:

  #include & lt; Iostream & gt; # MyVariant = boost :: variant & lt; Int, std :: string & gt; Use "boost \ variant \ variant.hpp"; Square type: converter: public boost: static_visitor & lt; & Gt; {Private: Temporary int _int; Mutable std :: string_string; Fixed variant converter (MyVariant & amp; v) {VariantConverter Res; V.apply_visitor (race); Withdrawal reserve; // Copy will disappear, right? } Public: Zero operator () (Int & amp; i) const {_int = i; } Zero operator () (std :: string & amp;; const {_string = s; } Fixed int to_int (MyVariant v) {Return Application (V) .from_int (); } Fixed std :: string to_string (MyVariant v) {return application (v) .from_string (); } Int to_int () {return _int; }; Std :: string from_string () {return_string; }; }; Int main () {namespace std; MyVariant v = 23; Int I = VariantConverter :: to_int (v); Cout & lt; & Lt; I & lt; & Lt; Endl; V = "Michael Jordan"; Std :: string s = VariantConverter :: to_string (v); Cout & lt; & Lt; S.c_str () & lt; & Lt; Endl; Cin.get (); Return 0; }   

I appreciate it if someone can guide me towards a better solution.

Or maybe someone could explain the logic behind me:

If I declare:

  MyVariant = boost :: variant & lt; Int, std :: string & gt;   

And then A:

  ConverterTaint: basic_suspector & lt; Int & gt; {Public: Int Operator (Int.) {Return Return; }; };   

Why is it that when I try to apply convertertools as a MyVariant:

  convertertoiint cti; MyVariant i = 10; I.apply_visitor (CTI);   

I get a compiler error about trying to find an operator () which takes the std :: string?

It seems that the application_visitor is trying to call the operator () can take MyVariant for each type. So what? If so, then why? How can I avoid this behavior?

Cheers!

You can avoid error convertertoiit to std :: What to do with string ? You may know that i can not be a std :: string , but the compiler is inappropriate to know that (and if it is true, then you Why Variant?).

apply_visitor will only call the correct operator () method, but it decides on time, and the possibility of generating code for the compiler .

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -