Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2006-04-15 05:40:41


Martin Adrian wrote:
> David Abrahams <dave <at> boost-consulting.com> writes:
>
> > Was there any discussion of the to_string(x) and string_to<T>(x)
> > alternative?
>
> I am using an interface which works like this
>
> to_string(T x);
> to_string(T x, const locale& l);
> // The following function always use the classic locale
> // this allows easy specializations for trivial cases like int
> to_string_classic(T x);
> [...]

What about converting to different string types, mainly std::string or std::wstring?

I am also working on implementing to_string and string_to :). Maybe we could combine our efforts and get to_string/string_to into Boost.

At the moment, my versions don't have locale support and string_to isn't returning optional<T>, although I am working on it. They can be used like this:

    print( boost::to_string< char >( 10 )); // returns std::string
    print( boost::to_string< char >( 10.01 ));
    print( boost::to_string< wchar_t >( 10 )); // returns std::wstring
    print( boost::to_string< wchar_t >( 10.01 ));

and:

    print( boost::string_to< int >( "10" ));
    print( boost::string_to< float >( "10.01" ));

to_string currently looks like this:

#ifndef BOOST_CONVERSION_TO_STRING_HPP
#define BOOST_CONVERSION_TO_STRING_HPP

#include <sstream>
#include <string>

namespace boost
{

namespace detail
{

template< typename String >
struct converter
{
    typedef String type;
};

template<>
struct converter< char >
{
    typedef std::string type;
};

template<>
struct converter< wchar_t >
{
    typedef std::wstring type;
};

}

/** @brief Represent the contents of an object as a string.
  *
  * @param value The object value to convert to a string.
  * @return The string representation of that object.
  */

template< typename String, typename T >
typename detail::converter< String >::type
to_string( const T & value )
{
    typedef typename detail::converter< String >::type string_type;
    typedef typename string_type::value_type char_type;
    typedef typename string_type::traits_type char_traits;
    typedef typename string_type::allocator_type allocator_type;

    std::basic_ostringstream< char_type, char_traits, allocator_type > converter;
    converter << value;
    return converter.str();
}

}

#endif

and is in a form I am reasonably happy with. The string_to implementation I am not yet happy with, but looks like this:

#ifndef BOOST_CONVERSION_STRING_TO_HPP
#define BOOST_CONVERSION_STRING_TO_HPP

#include <sstream>
#include <string>

namespace boost
{

template< typename T, typename CharT, typename CharTraits, typename Allocator >
T
string_to( const std::basic_string< CharT, CharTraits, Allocator > & str )
{
    std::basic_istringstream< CharT, CharTraits, Allocator > converter( str );
    T value;
    converter >> value;
    return value;
}

template< typename T >
T
string_to( const char * str )
{
    std::basic_istringstream< char > converter( str );
    T value;
    converter >> value;
    return value;
}

template< typename T >
T
string_to( const wchar_t * str )
{
    std::basic_istringstream< wchar_t > converter( str );
    T value;
    converter >> value;
    return value;
}

}

#endif

- Reece
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


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