D vs C++11
Malte Skarupke
malteskarupke at web.de
Sat Nov 3 16:30:56 PDT 2012
On Saturday, 3 November 2012 at 22:45:59 UTC, Tommi wrote:
> On Saturday, 3 November 2012 at 22:01:21 UTC, Malte Skarupke
> wrote:
>>
>> D also makes the const keyword more annoying than it should be.
>
> What kind of annoyances regarding const have you encountered in
> D?
To start off it's simple things like this:
void main()
{
struct A
{
this(int x) { this.x = x; }
int x;
}
const(A) a = A(5);
A b = a;
}
This doesn't compile. And it will probably never compile. The
issue is that struct A has a context pointer and because of
transitive const, you are not allowed to copy that pointer. And
you can not specify your own copy constructor because all copy
constructors happens post-blit, at which point you'd already have
a non-const pointer. So that will probably never change.
But that's not a big problem.
It's more been stuff like me implementing an equivalent of
std::function from C++. (as I said I like to be more explicit
about things than delegates) I uploaded the code for it here:
http://dpaste.dzfl.pl/60a46049 As you can see there isn't a
single mention of const in there. All I wanted was a const and an
immutable version of opCall and better const correctness for
opAssign, but I just couldn't get it to compile. It'd be great if
you could have a look at it. If you succeed in getting that code
to be const correct, please tell me how you did it. (Also worth
mentioning: I ran into at least two more issues in just this one
file: 1. I couldn't specify the templated opAssign that C++'s
std::function has because of a compiler bug (which will be fixed
in DMD 2.061) and I had to define opAssign twice because there is
no way to specify a function which accepts both an rvalue and an
lvalue) That being said the code still is much cleaner than it
would be in C++.
Another issue I've had was trying to implement my own hashtable
which has more deterministic memory behavior. In that I found it
very difficult to get the ranges for byKey and byValue to be
const correct. I think I had issues with the "inout" keyword when
I was passing pointers marked as inout to the range object. I
think I'll revisit that one tomorrow and maybe I'll then post
code here.
More information about the Digitalmars-d
mailing list