Is boost::lexical_cast<>() always bijective?

A

Alex Vinokur

Is boost::lexical_cast<>() always bijective?


In other words:

// --------------------------------------
template <class From, class To>
bool type_cast (const From& i_from, To& o_to)
{
bool ret = true;
try
{
o_to = boost::lexical_cast<To>(i_from);
}
catch(boost::bad_lexical_cast &)
{
ret = false;
}
return ret;

}


// --------------------------
T1 t1 = <some value>;
T2 t2;

if (type_cast (t1, t2))
{
assert (type_cast (t2, t1)); // does it always succeed?
}


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
M

Martin York

// --------------------------------------
template <class From, class To>
bool type_cast (const From& i_from, To& o_to)
{
bool ret = true;
try
{
o_to = boost::lexical_cast<To>(i_from);
}
catch(boost::bad_lexical_cast &)
{
ret = false;
}
return ret;

}

// --------------------------
T1 t1 = <some value>;
T2 t2;

if (type_cast (t1, t2))
{
assert (type_cast (t2, t1)); // does it always succeed?
}

No.


This only works if you have the following defined:
1) std::eek:stream& operator<<(std::eek:stream&,const From&)
2) std::istream& operator>>(std::istream&,const To)
3) (2) can actually decode the stream without throwing.
 
A

Abhishek Padmanabh


Are there any samples of non-biectivity of boost::lexical_cast<>() ?

[snipped]

Another could be an array (for example, cast from char[] to
std::string). :)
It decays to a pointer when you cast something out of it but you can
never cast that result back into an array.
 
J

James Kanze

Is boost::lexical_cast<>() always bijective?
In other words:
// --------------------------------------
template <class From, class To>
bool type_cast (const From& i_from, To& o_to)
{
bool ret = true;
try
{
o_to = boost::lexical_cast<To>(i_from);
}
catch(boost::bad_lexical_cast &)
{
ret = false;
}
return ret;
}
// --------------------------
T1 t1 = <some value>;
T2 t2;
if (type_cast (t1, t2))
{
assert (type_cast (t2, t1)); // does it always succeed?
}

Obviously not, since a user can define << and >> anyway he
pleases (or even define one, and not the other).

And that's not bijection. Bijection would guarantee a round
trip, with t1 equal to its initial value after the second
conversion, with all values in T1 having a mapping to a distinct
value in T2, and all values in T2 being the result of some
mapping from T1. Which, of course, is bound to fail as soon as
the types don't have the same number of values. Thus, string to
double may fail, and even when it succeeds, converting that
double back to string may result in a different value.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top