Boost logo

Boost :

From: Aleksandr Golovanov (silvery_at_[hidden])
Date: 2003-08-15 20:23:57


"Ross Smith" <rosss_at_[hidden]> wrote in message
news:bhgued$s99$1_at_sea.gmane.org...
> Aleksandr Golovanov wrote:
> > "Ross Smith" <rosss_at_[hidden]> wrote in message
> > news:bher8m$gnq$1_at_sea.gmane.org...
> >
> >>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.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk