Boost logo

Boost Users :

From: shams (shams_at_[hidden])
Date: 2006-08-04 00:53:10


Hi,

Here is a sample custom string class that I am tring to get working with
lexical_cast but it gives me compile errors. Can you please see what I am doing wrong here. More explanation below.

OTOH if I use a custom lexical_cast using wstringstream it works fine.

// main.cpp
#include <string>
#include <iostream>

#include "boost_1_33_1/boost/lexical_cast.hpp"

using namespace std;

class String : public wstring
{
public:
        String()
        {
        }

        String(const wchar_t *str) : wstring(str)
        {
        }
        
        String& operator=(const String& Rhs)
        {
                return static_cast<String&>(wstring::operator=(Rhs));
        }
};

// This is commented cause lexical_cast wants bool return value.
#if 0
template<class OutputStream>
OutputStream& operator<<(OutputStream &o, const String &s)
{
        return o << s.c_str();
}
#endif

template<class OutputStream>
bool operator<<(OutputStream &o, const String &s)
{
        o << s.c_str();

        return true;
}

// This is commented cause lexical_cast wants bool return value.
#if 0
template<class InputStream>
InputStream& operator>>(InputStream &i, String &s)
{
        wstring ws;

        i >> ws;

        s.assign(ws);

        return i;
}
#endif

template<class InputStream>
bool operator>>(InputStream &i, String &s)
{
        wstring ws;

        i >> ws;

        s.assign(ws);

        return true;
}

int main()
{
        // This line causes errors both on MSVC and GCC.
        wcout << boost::lexical_cast<String>(10);

        // These lines DONT cause errors and work ok.
        String s = L"hello";

        wcout << s;
        wcout << endl;

        wcin >> s;
        wcout << s;
        wcout << endl;

        return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net