user defined literals

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 25 06:55:42 PDT 2011


On Fri, 25 Mar 2011 09:32:12 -0400, Trass3r <un at known.com> wrote:

> 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?

What is the need for this?  Just to replace complex(0, 5) with 5i?  I  
don't see the huge benefit.

Also, complex is the clear and obvious beneficiary because it's so widely  
understood.  What are other use cases?  BigXXX for sure, but Rational  
probably cannot be covered by this since you need two numbers.

I simply find the complication of the language is not worth the benefits.

-Steve


More information about the Digitalmars-d mailing list