Boost logo

Boost Users :

From: Johan Råde (rade_at_[hidden])
Date: 2006-07-30 05:01:53


Johan Råde wrote:

Now I have improved the macro so that one can write

     template<class CharType> void f()
     {
         CharType c = S_('a');

         basic_string<CharType> s = S_("a");
     }

and the code will compile both for CharType = char and CharType = wchar_t.

Why is this relevant to Boost?

There are several Boost libraries that deal with strings that are
templatized on the character type. Such libraries should come with tests
that also are templatized on the character type. But when you write such
tests, it is convenient to have a version of char s = 'a' and string
s = "a" that works in templates.

--Johan Råde

-------------------------------------

Here is the improved macro:

#define S_(a) make_literal_helper(a, L##a)

class char_literal_helper {
public:
        char_literal_helper(char c, wchar_t wc) : c_(c), wc_(wc) {}
        operator char() { return c_; }
        operator wchar_t() { return wc_; }
private:
        const char c_;
        const wchar_t wc_;
};

class string_literal_helper {
public:
        string_literal_helper(const char* s, const wchar_t* ws) : s_(s), ws_(ws) {}
        operator std::string() { return s_; }
        operator std::wstring() { return ws_; }
private:
        const char* s_;
        const wchar_t* ws_;
};

inline char_literal_helper make_literal_helper(char c, wchar_t wc)
{
        return char_literal_helper(c, wc);
}

inline string_literal_helper make_literal_helper(char* s, wchar_t* ws)
{
        return string_literal_helper(s, ws);
}


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