"Ross Smith" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Aleksandr Golovanov wrote:
> > "Ross Smith" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> >
> >>Aleksandr Golovanov wrote:
> >>
> >>>Yesterday, I ran into a small problem, lexical_cast accepts copy
instead of
> >>>(const)? reference to a source. I have a class which I would prefer to
be
> >>>noncopyable and castable with laxical_cast at the same time.
> >>
> >>Wrap the object in boost::cref().
> >
> > Unfortunately, cref won't work because lexical_cast propagates the
source
> > type as a template parameter to various helper/standard templates:
>
> You're wrong; it works perfectly well. I tried it before I posted the
> suggestion.
>
Following working example shows one possible case when application of
boost::cref leads to a wrong result.
Compiler VC6 SP5; lexical_cast from 1.30.0 boost release.
#include "boost/lexical_cast.hpp"
#include "boost/ref.hpp"
#include <iostream>
#include <string>
#include <limits>
class big_decimal
{
public:
big_decimal() : m_val( 1.23456789 ) {}
public:
double m_val;
};
namespace std {
class numeric_limits<big_decimal>
: public numeric_limits<double>
{
};
}
std::ostream& operator<<( std::ostream& s, big_decimal const& arg )
{
return s << arg.m_val;
}
int main()
{
big_decimal dec;
std::cout << boost::lexical_cast<std::string>( dec ) << "\n";
std::cout << boost::lexical_cast<std::string>( boost::cref( dec ) ) <<
"\n";
return 0;
}
Result of execution:
1.23456789
1.23457
Conclusion: usage of boost::cref with lexical_cast may lead to wrong results
in the current implementation.
--
Thanks.
Aleksandr Golovanov,
MetaCommunications Engineering.
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost