SRC (C++) : stream_cast
От: Igor Soukhov  
Дата: 22.09.02 18:23
Оценка: 37 (5)
//(C) Dietmar Kuehl

#include <sstream>
template <typename T, typename S>
T stream_cast(S const& val) {
    std::stringstream stream;
    stream << val;
    T rc;
    stream >> rc;
    return rc;
}
* thriving in a production environment *
Re: SRC (C++) : stream_cast
От: Chorkov Россия  
Дата: 24.09.02 06:35
Оценка: 6 (1)
Здравствуйте Igor Soukhov, Вы писали:

IS> [sciped]


Нечто похожее ...
template <typename T>
class stream_cast_2
{
    T rc;
public:
    template <class C>
    inline stream_cast_2(const C& val)
    {
        std::stringstream stream;
        stream << val;
        stream >> rc;
    };

    inline operator T(void)
    {
        return rc;
    };

};


P.S.
Жаль что следующие не компилируется
class x_cast
{
    std::stringstream stream;
public:
    template <class C>
    inline x_cast(const C& val)
    {
        stream << val;
    };

    template <class T>
    inline operator T (void)
    {
        T return_value;
        stream >> return_value;
        return return_value;
    };
};
Re: SRC (C++) : stream_cast
От: m.a.g. Мальта http://dottedmag.net/
Дата: 24.09.02 09:20
Оценка:
Здравствуйте Igor Soukhov, Вы писали:

IS>template <typename T, typename S>

IS>T stream_cast(S const& val) {

Уже пристутствует в библиотеке

boost::lexical_cast
Re[2]: SRC (C++) : stream_cast
От: Igor Soukhov  
Дата: 24.09.02 09:25
Оценка:
Здравствуйте m.a.g., Вы писали:

IS>>template <typename T, typename S>

IS>>T stream_cast(S const& val) {

MAG>Уже пристутствует в библиотеке


MAG>boost::lexical_cast


я знаю — и что ?
* thriving in a production environment *
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.