Type inference and overloaded functions

Jonathan M Davis jmdavisProg at gmx.com
Tue Dec 10 01:28:12 PST 2013


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


More information about the Digitalmars-d-learn mailing list