On Thursday, 16 February 2017 at 07:34:02 UTC, Jacob Carlborg wrote:
On 2017-02-16 01:37, ZombineDev wrote:

BTW, shouldn't we use `enum`, instead of `auto`, since everywhere else `enum` means guaranteed to be computed at compile-time whereas `auto`
means the opposite?

Far enough, since it's not possible to change the parameter inside the function anyway.

Question though, what happens with an array literal, example:

void foo(int[] a = [1, 2, 3])()
    auto b = a; // allocation ?
    auto c = a; // allocation ?

As far as I understand, if an array is declared as a manifest constant it will cause a new allocation for each time it's used.

enum a = [1, 2, 3];
auto b = a; // new allocation
auto c = a; // new allocation

What happens when an array literal is a default value for a template parameter?

enums are just literals - values without identity. This means that if you need them at runtime, the compiler will need to allocate storage for each time they are used. In cases where such a values is used in more places, it may be more economical to assign it to a static immutable variable and reference it in place of the enum.

In the case of template parameters I would expect this to be even more true, because an array by definition is a contiguous chunk of memory and a template parameter is something encoded in the mangled name of the symbol at runtime, so the compiler can't get away from allocating additional storage for each usage of the template value parameter at runtime.

(@compiler devs, please correct me if I am wrong)

Reply via email to