Monday, May 24, 2010

User-Defined Literals, a new feature in C++0x

Going through the latest draft. There's going to be a way to define your own custom literals.

For example:


long double operator "" w(long double);
std::string operator "" w(const char16_t*, size_t);
unsigned operator "" w(const char*);

int main()
{
1.2w; // calls operator "" w(1.2L)
u"one"w; // calls operator "" w(u"one", 3)
12w; // calls operator "" w("12")
"two"w; // error: no applicable literal operator
}


Could come in handy...

No comments: