[xsd-users] Re: error: cannot convert to ?int? in assignment
Boris Kolpackov
boris at codesynthesis.com
Mon Feb 21 08:57:20 EST 2011
Hi Shubhangi,
shubhgd at cs.vt.edu <shubhgd at cs.vt.edu> writes:
> <xs:simpleType name="scalartype">
> <xs:union memberTypes="xs:int xs:double xs:string xs:boolean"/>
> </xs:simpleType>
>
> [...]
>
> However, with this piece of code, I get the following error:
> error: cannot convert ?const scalartype? to ?int? in assignment
In C++/Tree union XML Schema types are mapped to C++ classes derived
from std::string. So what you get is a lexical representation of the
value. If you want to convert it to one of the member types, you will
need to do it yourself, for example:
#include <sstream>
using namespace std;
scalartype v = ...;
int i;
istringstream is (v);
if (!is >> i)
{
// Error: value is not an int.
}
Or, if you are using Boost, you can shorten the above code with
lexical_cast:
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
scalartype v = ...;
int i = lexical_cast<int> (v);
Boris
More information about the xsd-users
mailing list