user defined literals

Trass3r un at known.com
Fri Mar 25 06:32:12 PDT 2011


Just came across C++0x user defined literals (via user defined suffixes): http://en.wikipedia.org/wiki/C%2B%2B0x#User-defined_literals

They are implemented via something like:

std::complex<double> operator "" i(double d) // cooked form, e.g. 15i
{ 
    return std::complex(0, d);
}

Another C++0x typical <nasty syntax> solution for a nice feature.
It should be possible to incorporate this into D quite easily via something along the lines of:

Complex!T opSuffix(U data) // whatever the name should be

where U can be a number type or a string (see the link for cooked vs. raw form)

Just like traditional operator overloading in D this would only mean a tiny layer of syntactic sugar on top of standard language features. Given D's CTFE capabilities you could probably even define compile-time literals without further modifications (C++0x needs variadic templates and constexpr for that! :D).

My actual question is: are there any non-apparent pitfalls or side effects that might complicate or speak against an implementation?


More information about the Digitalmars-d mailing list