| Thread overview | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 09, 2016 A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Direct link: https://maikklein.github.io/post/CppAndD/ Reddit link: https://www.reddit.com/r/programming/comments/49lna6/a_comparison_between_c_and_d/ If you spot any mistakes, please let me know. | ||||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to maik klein | On Wed, 09 Mar 2016 01:18:26 +0000, maik klein wrote: > If you spot any mistakes, please let me know. > structs in D don’t have a default constructor because every type needs exception free default construction Rather, declaring a variable should never throw an exception. Declaring a variable shouldn't create any nontrivial work. It's trivial to create a type in D that doesn't have exception free default construction: class A { this() { throw new Exception(); } } > C++ has a static_cast while D does not. D has one explicit cast operator. It can do much of what static_cast does, though D does not let you downcast without a runtime check. > Exceptions in D require the GC. You don't need the GC; you just need storage that's not on a stack frame below where you're catching the exception. You can malloc an exception and throw it. You can allocate space for an exception in the static data region and throw it from there, like with OutOfMemoryError. You can allocate space for an exception on the stack in main() and pass it down the call chain. > To get foo.bar.baz you can create baz.d inside the bar folder and bar inside the foo folder. That's the canonical way of doing it, but with dub, I'm seeing people generally adding the project name before that. So, for instance, I have module "roguelike.levelgen" in source file "source/levelgen.d". It works. > D can print any type at compile time or runtime with > writeln(SomeType.stringof) or writeln(typeof(somevar).stringof) stringof evaluates an expression or type and produces a string representation for that value. That string representation is a compile- time constant. It doesn't print it at compile time or runtime. Does writeln print values at compile time? That would be kind of strange. Maybe useful in the context of CTFE, but still a little unexpected. | |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Chris Wright | On Wednesday, 9 March 2016 at 02:14:34 UTC, Chris Wright wrote: > On Wed, 09 Mar 2016 01:18:26 +0000, maik klein wrote: >> [...] > >> [...] > > Rather, declaring a variable should never throw an exception. Declaring a variable shouldn't create any nontrivial work. > > It's trivial to create a type in D that doesn't have exception free default construction: > > class A { > this() { throw new Exception(); } > } That is why I have limited it to `structs`, but I see that I have used `every type` before which is just wrong. Not sure why I even wrote it. >> [...] > > D has one explicit cast operator. It can do much of what static_cast does, though D does not let you downcast without a runtime check. I should probably be more clear that this is about compile time. >> [...] > > You don't need the GC; you just need storage that's not on a stack frame below where you're catching the exception. > > You can malloc an exception and throw it. > > You can allocate space for an exception in the static data region and throw it from there, like with OutOfMemoryError. > > You can allocate space for an exception on the stack in main() and pass it down the call chain. Thanks, I did not know this. Will fix it asap. >> [...] > > That's the canonical way of doing it, but with dub, I'm seeing people generally adding the project name before that. So, for instance, I have module "roguelike.levelgen" in source file "source/levelgen.d". It works. > >> [...] > > stringof evaluates an expression or type and produces a string representation for that value. That string representation is a compile- time constant. It doesn't print it at compile time or runtime. > > Does writeln print values at compile time? That would be kind of strange. Maybe useful in the context of CTFE, but still a little unexpected. I should be more clear. I meant writeln for runtime printing and pragma(msg) for compile time printing. | |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to maik klein | On Wednesday, 9 March 2016 at 01:18:26 UTC, maik klein wrote: > Direct link: https://maikklein.github.io/post/CppAndD/ > Reddit link: https://www.reddit.com/r/programming/comments/49lna6/a_comparison_between_c_and_d/ > > If you spot any mistakes, please let me know. > D moves objects with a bitwise copy, this means you should not have internal pointers. Unless you define this(this) right? > version(YourKeywork){...}. Should be "Keyword". | |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | On Wednesday, 9 March 2016 at 03:04:31 UTC, Jack Stouffer wrote: > On Wednesday, 9 March 2016 at 01:18:26 UTC, maik klein wrote: >> Direct link: https://maikklein.github.io/post/CppAndD/ >> Reddit link: https://www.reddit.com/r/programming/comments/49lna6/a_comparison_between_c_and_d/ >> >> If you spot any mistakes, please let me know. > >> D moves objects with a bitwise copy, this means you should not have internal pointers. > > Unless you define this(this) right I don't think so. You can read more about it here https://dlang.org/phobos/std_algorithm_mutation.html#move > >> version(YourKeywork){...}. > > Should be "Keyword". Thanks not sure why this wasn't highlighted as an error. | |||
March 08, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Chris Wright | On 3/8/2016 6:14 PM, Chris Wright wrote:
> D does not let you downcast without a runtime check.
You can by casting to void* first, then the downcast.
| |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Wednesday, 9 March 2016 at 04:15:39 UTC, Walter Bright wrote: > On 3/8/2016 6:14 PM, Chris Wright wrote: >> D does not let you downcast without a runtime check. > > You can by casting to void* first, then the downcast. Thanks I'll update the post later. Does this mean I could do any cast at compile time? In encountered the problem here to give some context https://stackoverflow.com/questions/35694701/is-it-possible-to-cast-foo-to-ubytesize-at-compile-time Basically I wanted to cast some T to ubyte[size] at compile time. It seems that casting to void* is not a problem, but then upcasting from void* to ubyte[size] is still not allowed at compile time. If that is not possible maybe I can just use a union instead. | |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to maik klein | On Wednesday, 9 March 2016 at 04:37:47 UTC, maik klein wrote: > Does this mean I could do any cast at compile time? No, compile time has other restrictions. http://dlang.org/spec/function.html#interpretation "Any pointer may be cast to void * and from void * back to its original type. Casting between pointer and non-pointer types is prohibited. " I don't think compile time execution allows unions, either. | |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to maik klein | On 03/08/2016 08:18 PM, maik klein wrote:
> Direct link: https://maikklein.github.io/post/CppAndD/
> Reddit link:
> https://www.reddit.com/r/programming/comments/49lna6/a_comparison_between_c_and_d/
>
>
> If you spot any mistakes, please let me know.
Nice work, thanks! -- Andrei
| |||
March 09, 2016 Re: A comparison between C++ and D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to maik klein | On Wednesday, 9 March 2016 at 01:18:26 UTC, maik klein wrote:
> Direct link: https://maikklein.github.io/post/CppAndD/
> Reddit link: https://www.reddit.com/r/programming/comments/49lna6/a_comparison_between_c_and_d/
>
> If you spot any mistakes, please let me know.
C++ as well as D have anonymous functions. C++: [](auto a, auto b){ return a + b;} , D: (a, b) => a + b or (a, b){return a + b;}. As far as I know capturing other variables requires the GC in D. In C++ you can explicitly capture variables by copy, ref or move. Lambda functions in D can not return references. C++17 will also make lambda functions available with constexpr. Lambda functions can also be used at compile time in D.
Is this really true? Couldn't the closure be stored internally somewhere like std::function<> does in C++?
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply