June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On 06/03/2017 10:29 PM, Stanislav Blinov wrote:
> Meep. Wrong :)
>
> Static initializers for static variables and constants are evaluated at compile time, initializing them with runtime values is a compile-time error.
Meep. Meep. I wouldn't say you're wrong, but there's nitpicking to be done.
You can't use a run-time value as the initializer, but you *can* initialize a static variable with a run-time value: using a static constructor.
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Saturday, 3 June 2017 at 21:04:16 UTC, ag0aep6g wrote:
> On 06/03/2017 10:29 PM, Stanislav Blinov wrote:
>> Meep. Wrong :)
>>
>> Static initializers for static variables and constants are evaluated at compile time, initializing them with runtime values is a compile-time error.
>
> Meep. Meep. I wouldn't say you're wrong, but there's nitpicking to be done.
>
> You can't use a run-time value as the initializer, but you *can* initialize a static variable with a run-time value: using a static constructor.
Mental note to self: never write assuming sentences in a NG :)
You're correct, of course. I meant inline initialization, as presented in the code in question.
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Saturday, 3 June 2017 at 19:12:46 UTC, Steven Schveighoffer wrote:
> On Saturday, 3 June 2017 at 17:32:41 UTC, Andrei Alexandrescu wrote:
>> On 06/03/2017 01:03 PM, Russel Winder via Digitalmars-d wrote:
>>> Björn Fahller has done compile time sort in C++17 here http://playfulpr
>>> ogramming.blogspot.co.uk/2017/06/constexpr-quicksort-in-c17.html
>>>
>>> Surely D can do better?
>>
>> There is nothing to do really. Just use standard library sort.
>>
>> void main() {
>> import std.algorithm, std.stdio;
>> enum a = [ 3, 1, 2, 4, 0 ];
>> static auto b = sort(a);
>> writeln(b);
>> }
>
> I'd say this deserves a blog post but it would be too short.
>
> -Steve
Title would be longer:
Don't bring a knife to a gun fight especially if it is a knife made of a spoon, a toothbrush, duct tape and a shoelace
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 06/03/2017 12:12 PM, Steven Schveighoffer wrote: > I'd say this deserves a blog post but it would be too short. I made many good friends at C++Now. Some of them know Atila from CppCon and other C++ conferences. (Beer involved. :) ) They told me Atila would routinely tell them during C++ presentations "This wouldn't be a talk at a DConf; it's a language feature in D." :) Ali |
June 04, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On 03.06.2017 22:16, Basile B. wrote: > On Saturday, 3 June 2017 at 19:12:46 UTC, Steven Schveighoffer wrote: >> On Saturday, 3 June 2017 at 17:32:41 UTC, Andrei Alexandrescu wrote: >>> On 06/03/2017 01:03 PM, Russel Winder via Digitalmars-d wrote: >>>> Björn Fahller has done compile time sort in C++17 here http://playfulpr >>>> ogramming.blogspot.co.uk/2017/06/constexpr-quicksort-in-c17.html >>>> >>>> Surely D can do better? >>> >>> There is nothing to do really. Just use standard library sort. >>> >>> void main() { >>> import std.algorithm, std.stdio; >>> enum a = [ 3, 1, 2, 4, 0 ]; >>> static auto b = sort(a); >>> writeln(b); >>> } >> >> I'd say this deserves a blog post but it would be too short. >> >> -Steve > > Yes but let's correct the mistake first ;-] > ... There is no mistake. (But 'auto' is redundant.) > void main() { > import std.algorithm, std.stdio; > enum a = [ 3, 1, 2, 4, 0 ]; > enum b = sort(a);// static is not CT !!!!! > static assert(b[0] == 0); // does not pass with static auto b... > writeln(b); > } > > This is worse. Now there is an allocation at runtime. |
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Saturday, 3 June 2017 at 22:18:06 UTC, Timon Gehr wrote:
>
> There is no mistake. (But 'auto' is redundant.)
>
>> void main() {
>> import std.algorithm, std.stdio;
>> enum a = [ 3, 1, 2, 4, 0 ];
>> enum b = sort(a);// static is not CT !!!!!
>> static assert(b[0] == 0); // does not pass with static auto b...
>> writeln(b);
>> }
>>
>>
>
> This is worse. Now there is an allocation at runtime.
So maybe we can do a blog post on when to use static or enum? Because smart people don't seem to be agreeing on this...
|
June 04, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Saturday, 3 June 2017 at 20:29:04 UTC, Stanislav Blinov wrote:
> On Saturday, 3 June 2017 at 20:18:59 UTC, Basile B. wrote:
>> On Saturday, 3 June 2017 at 18:45:56 UTC, Jacob Carlborg wrote:
>>> On 2017-06-03 20:31, Russel Winder via Digitalmars-d wrote:
>>>
>>>> But is this sort guaranteed to happen at compile time rather than
>>>> runtime?
>>>
>>> Yes. It's the context that decides if it occurs at compile time or at runtime.
>>>
>>> Something declared as "static" or "enum" requires that the value can be evaluated at compile time.
>>
>> Meeep. Wrong. The example is just wrong. 'static auto b = ...' is not a compile-time variable. It's just a variable that's like a global but declared within a function.
>> Remember the singleton pattern using 'static Stuff instance'.
>
> Meep. Wrong :)
>
> Static initializers for static variables and constants are evaluated at compile time, initializing them with runtime values is a compile-time error.
> Yes, you can't do a static assert on b. It's still initialized at compile time though.
Actually i know where my error comes from. It comes from "static immutable".
Since "static immutable" can be used in place of "enum" i've assumed that this was the real intention and not "static auto...".
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On 06/03/2017 07:41 PM, jmh530 wrote:
> On Saturday, 3 June 2017 at 22:18:06 UTC, Timon Gehr wrote:
>>
>> There is no mistake. (But 'auto' is redundant.)
>>
>>> void main() {
>>> import std.algorithm, std.stdio;
>>> enum a = [ 3, 1, 2, 4, 0 ];
>>> enum b = sort(a);// static is not CT !!!!!
>>> static assert(b[0] == 0); // does not pass with static auto b...
>>> writeln(b);
>>> }
>>>
>>>
>>
>> This is worse. Now there is an allocation at runtime.
>
> So maybe we can do a blog post on when to use static or enum? Because smart people don't seem to be agreeing on this...
Yes please! cc Mike. -- Andrei
|
June 04, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Saturday, 3 June 2017 at 23:41:37 UTC, jmh530 wrote:
> On Saturday, 3 June 2017 at 22:18:06 UTC, Timon Gehr wrote:
>>
>> There is no mistake. (But 'auto' is redundant.)
>>
>>> void main() {
>>> import std.algorithm, std.stdio;
>>> enum a = [ 3, 1, 2, 4, 0 ];
>>> enum b = sort(a);// static is not CT !!!!!
>>> static assert(b[0] == 0); // does not pass with static auto b...
>>> writeln(b);
>>> }
>>>
>>>
>>
>> This is worse. Now there is an allocation at runtime.
>
> So maybe we can do a blog post on when to use static or enum? Because smart people don't seem to be agreeing on this...
In General, prefer static immutable.
|
June 04, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sunday, 4 June 2017 at 03:28:32 UTC, Andrei Alexandrescu wrote: > On 06/03/2017 07:41 PM, jmh530 wrote: >> On Saturday, 3 June 2017 at 22:18:06 UTC, Timon Gehr wrote: >>> >>> There is no mistake. (But 'auto' is redundant.) >>> >>>> void main() { >>>> import std.algorithm, std.stdio; >>>> enum a = [ 3, 1, 2, 4, 0 ]; >>>> enum b = sort(a);// static is not CT !!!!! >>>> static assert(b[0] == 0); // does not pass with static auto b... >>>> writeln(b); >>>> } >>>> >>>> >>> >>> This is worse. Now there is an allocation at runtime. >> >> So maybe we can do a blog post on when to use static or enum? Because smart people don't seem to be agreeing on this... > > Yes please! cc Mike. -- Andrei Any volunteers? This one surprised me. Looking at the disassembly over at godbolt, I see this when using enum b = sort(a): ``` lea rax, [rbp-64] mov QWORD PTR [rax], 0 mov QWORD PTR [rax+8], 0 mov QWORD PTR [rbp-48], 0 mov QWORD PTR [rbp-40], 0 mov DWORD PTR [rbp-32], 0 mov DWORD PTR [rbp-44], 1 mov DWORD PTR [rbp-40], 2 mov DWORD PTR [rbp-36], 3 mov DWORD PTR [rbp-32], 4 mov esi, 5 mov edi, OFFSET FLAT:_D11TypeInfo_Ai6__initZ call _d_arrayliteralTX mov rdx, QWORD PTR [rbp-48] mov QWORD PTR [rax], rdx mov rdx, QWORD PTR [rbp-40] mov QWORD PTR [rax+8], rdx mov edx, DWORD PTR [rbp-32] mov DWORD PTR [rax+16], edx mov QWORD PTR [rbp-64], 5 mov QWORD PTR [rbp-56], rax mov rax, QWORD PTR [rbp-64] mov rdx, QWORD PTR [rbp-56] mov rcx, rax mov rbx, rdx mov rax, rdx mov rdi, rcx mov rsi, rax call writeln ``` And this when using static b = sort(a) (and the same with static immutable b): ``` mov rdx, QWORD PTR fs:std.range.SortedRange!(int[], "a < b").SortedRange example.main().b@tpoff mov rax, QWORD PTR fs:std.range.SortedRange!(int[], "a < b").SortedRange example.main().b@tpoff+8 mov rdi, rdx mov rsi, rax call writeln ``` I would not have expected enum b = sort(a) to trigger an allocation. auto b, yes, of course (and the disassembly from that is not much different). So I'd love to see a blog post explaining it. |
Copyright © 1999-2021 by the D Language Foundation