June 03, 2017 C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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? -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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);
}
Andrei
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu Attachments:
| On Sat, 2017-06-03 at 13:32 -0400, Andrei Alexandrescu via Digitalmars- d wrote: > […] > > 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); > } > But is this sort guaranteed to happen at compile time rather than runtime? -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | On Saturday, 3 June 2017 at 18:31:37 UTC, Russel Winder wrote: > On Sat, 2017-06-03 at 13:32 -0400, Andrei Alexandrescu via Digitalmars- d wrote: >> […] >> >> 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); >> } >> > > But is this sort guaranteed to happen at compile time rather than runtime? Yes: https://dlang.org/spec/function.html#interpretation And there's no jumping through ridiculous hoops, unlike the article. |
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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. That's the beauty of D, the same functions can be used both at compile time and at runtime. -- /Jacob Carlborg |
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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
|
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
It already looks like a great blog post as is :)
|
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
Yes but let's correct the mistake first ;-]
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);
}
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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'.
|
June 03, 2017 Re: C++17 cannot beat D surely | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | 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.
|
Copyright © 1999-2021 by the D Language Foundation