Jump to page: 1 2
Thread overview
Why this code can't take advantage from CTFE?
Feb 03, 2016
Andrea Fontana
Feb 03, 2016
anonymous
Feb 03, 2016
Adam D. Ruppe
Feb 03, 2016
Messenger
Feb 03, 2016
Adam D. Ruppe
Feb 03, 2016
Andrea Fontana
Feb 03, 2016
Messenger
Feb 03, 2016
Marc Schütz
Feb 03, 2016
Andrea Fontana
Feb 03, 2016
Timon Gehr
Feb 05, 2016
Andrea Fontana
Feb 05, 2016
Luis
Feb 05, 2016
Andrea Fontana
February 03, 2016
This code:

import std.stdio;

int very_very_long_function(in int k)
{
        if (!__ctfe) writeln("Can't use ctfe!");
        return k/2;
}

void main()
{
        enum first = very_very_long_function(10);
        writeln("First is ", first);

        auto second = very_very_long_function(12);
        writeln("Second is ", second);

        auto third = first;
        third += 1;
        writeln("Third is ", third);
}

Why second init doesn't work with CTFE? It could be something like third, but with one less copy. What am I missing?
February 03, 2016
On 03.02.2016 16:34, Andrea Fontana wrote:
> void main()
> {
>          enum first = very_very_long_function(10);
>          writeln("First is ", first);
>
>          auto second = very_very_long_function(12);
>          writeln("Second is ", second);
>
>          auto third = first;
>          third += 1;
>          writeln("Third is ", third);
> }
>
> Why second init doesn't work with CTFE? It could be something like
> third, but with one less copy. What am I missing?

The compiler doesn't try to apply CTFE everywhere, because it could take forever. So CTFE is only done when the programmer requests it by using a function in a static context.

The compiler is still free to precompute the value at compile time, if it thinks that's a good idea, but that's just an optimization. And while it's evaluation of a function at compile time, it's not CTFE in the sense of __ctfe.
February 03, 2016
On Wednesday, 3 February 2016 at 15:34:51 UTC, Andrea Fontana wrote:
>         enum first = very_very_long_function(10);
>         auto second = very_very_long_function(12);
>
> Why second init doesn't work with CTFE?

You never asked for CTFE.

CTFE only happens when it *has* to - when you write code that specifically asks for or requires it.

Ordinary variables don't require ctfe, so they don't get it.
February 03, 2016
On Wednesday, 3 February 2016 at 15:59:43 UTC, Adam D. Ruppe wrote:
> You never asked for CTFE.
>
> CTFE only happens when it *has* to - when you write code that specifically asks for or requires it.

What is a good way to try to force it? Using enum? Then optionally copying the value once to avoid the "manifest constant" copy/paste behaviour, where applicable?

February 03, 2016
On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote:
> What is a good way to try to force it? Using enum? Then optionally copying the value once to avoid the "manifest constant" copy/paste behaviour, where applicable?

Yes, or static local variables are ctfe initialized too.
February 03, 2016
On Wednesday, 3 February 2016 at 16:24:19 UTC, Adam D. Ruppe wrote:
> On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote:
>> What is a good way to try to force it? Using enum? Then optionally copying the value once to avoid the "manifest constant" copy/paste behaviour, where applicable?
>
> Yes, or static local variables are ctfe initialized too.

But if I want a normal var, not static or enum?
There's no way to force it?
February 03, 2016
On Wednesday, 3 February 2016 at 17:16:30 UTC, Andrea Fontana wrote:
> But if I want a normal var, not static or enum?
> There's no way to force it?

Just make a mutable copy of it, I think.
February 03, 2016
On 2/3/16 12:16 PM, Andrea Fontana wrote:
> On Wednesday, 3 February 2016 at 16:24:19 UTC, Adam D. Ruppe wrote:
>> On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote:
>>> What is a good way to try to force it? Using enum? Then optionally
>>> copying the value once to avoid the "manifest constant" copy/paste
>>> behaviour, where applicable?
>>
>> Yes, or static local variables are ctfe initialized too.
>
> But if I want a normal var, not static or enum?
> There's no way to force it?

Hm... trick you could do is:

auto myvar = AliasSeq!(someCTFEFunc())[0];

-Steve
February 03, 2016
On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote:
> What is a good way to try to force it? Using enum? Then optionally copying the value once to avoid the "manifest constant" copy/paste behaviour, where applicable?

template forceCTFE(alias expr) {
    alias forceCTFE = expr;
}

auto value = forceCTFE!(some_expression());
February 03, 2016
On Wednesday, 3 February 2016 at 17:49:39 UTC, Marc Schütz wrote:
> On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote:
>> What is a good way to try to force it? Using enum? Then optionally copying the value once to avoid the "manifest constant" copy/paste behaviour, where applicable?
>
> template forceCTFE(alias expr) {
>     alias forceCTFE = expr;
> }
>
> auto value = forceCTFE!(some_expression());

Interesting :)
Thank you!
« First   ‹ Prev
1 2