const(Class) is mangled as Class const* const
Jerry via Digitalmars-d
digitalmars-d at puremagic.com
Sun Mar 26 15:56:59 PDT 2017
On Sunday, 26 March 2017 at 22:29:56 UTC, deadalnix wrote:
> It is clear that you won't be able to express 100% of C++ in D,
> that would require to important all the weird parts of C++ into
> D, but if we are doing so, why use D in the first place ?
>
> Note that using const Class* in C++ is essentially useless. The
> class remains mutable and the reference is local the the callee
> anyway, so it doesn't change anything for the caller. Such a
> pattern is most likely indicative of a bug on the C++ side, or
> at least of code that do not do what the author intend to.
For `const Class*` the Class is not mutable. It is the case of
`Class* const` that Class is mutable. I see a lot of people in D
say similar things about it. Saying it is a bug, saying it's a
good thing that a const pointer with mutable type isn't in D. Yet
they always tend to be the people that have never actually used
C++. As is indicative of not even knowing the correct syntax to
use in C++. A common matter in C++ is to use templates, you may
have seen it, it's a really common pattern, `const T&`. The magic
that makes this work is that you read it like this: `T const &`.
They both mean the samething but I find makes more sense,
conceptually to read it like that. Now substitute a type for T,
`int* const &`. You see here that int is mutable for this
template. You can see my other post about cmp(), and how Phobos
avoids usages of const as it just makes writing generic code
difficult.
More information about the Digitalmars-d
mailing list