Type inference and overloaded functions

Namespace rswhite4 at googlemail.com
Tue Dec 10 01:33:33 PST 2013


On Tuesday, 10 December 2013 at 09:28:27 UTC, Jonathan M Davis 
wrote:
> On Tuesday, December 10, 2013 10:10:22 Namespace wrote:
>> Yeah I remember, but Kenji made a Pull Request to change this.
>> Regardless it would be very useful to have static array 
>> literals.
>
> It should be possible to do that via a templated function which 
> takes a static
> array and then returns it. e.g.
>
> auto staticLiteral(T, size_t n)(T[n] literal)
> {
>     return literal;
> }
>
> auto staticArray = staticLiteral([1, 2, 3, 4]);
>
> The compiler should optimize out the heap allocation, since the 
> literal is
> directly converted to a static array (it might not optimize it 
> now, but it
> definitely should, in which case, you're effectively creating a 
> static array
> literal without any heap allocations).
>
> Maybe that's more verbose than would be ideal, but it allows us 
> to essentially
> have static array literals without having to add anything to 
> the language.
>
> - Jonathan M Davis
Ugly. Why not {1, 2, 3} or [1, 2, 3]s. The compiler could (as 
long as necessary) rewrite this to your ugly solution.
But library solutions are always ugly and mostly bad and bug 
prone. See scoped, Typedef and destroy.


More information about the Digitalmars-d-learn mailing list