Add C++ Const Mangling DIP#143
Conversation
|
I really like the syntax On the other hand, I find it very confusing to write
Using the terminology of D's grammar, in D, You propose a new postfix type constructor Implementing the type wouldn't be that difficult at all. Basically, struct FinalPtr(T)
{
private T* ptr;
alias ptr this;
@disable:
this();
void opAssign(T...)(T);
void opOpAssign(string op, T...)(T);
T* opUnary(string op)() if (op == '+' || op == '-');
}An alternative to proposing a new type constructor might be the possibility to mangle struct/class templates in some generic manner. Unfortunately, I don't know much about mangling, so I have no idea how viable this alternative actually is. |
|
Yah I wasn't sure about changing the behavior of const compared to C++ but also didn't want to break that It would also need to allow for const at the end. Or at least some way of handling this case: void foo(T)(T const p);
foo!(void*)( null ); // mangled as `void const(*)` as it does in C++For mangling this would only be for C++, so it would have to go about the way C++ does it. C++ has different mangling for every possible variation, even though it only allows one overload to exist with that specific number of indirections. // C++
void foo(void*);
void foo(void* const); // error redefinition of foo |
|
I'm not sure I understand what you mean. First, you're right that suggesting As stated, if void foo(T)(T const p);is still ill-formed (I'm not 100% sure that There is no way to make a template simply replace foo(T)(HeadConstPtr!T p);
alias HeadConstPtr(T : T*) = T const(*);.Then Introducing a non-assignable pointer type constructor is the only sane way I see. // C++
int *const *pp;// D
extern(C++) const(int*)* pp;Looks fine, but isn't. While changes made by the C++ program won't interfere with the D part, the other way around is well possible. // D
static immutable int* ip = new immutable int(1);
void fun()
{
pp = &ip; // fine in D: immutable(int*) casts to const(int*).
}// C++
void gun()
{
**pp = 2; // overwrites D's immutable value.
}Without the ability to express that, the type system cannot guarantee correctness. And currently, there is nothing that expresses C++'s There is also a bit of asymmetry as there is |
|
I don't really follow what you are saying,
This is not the purpose of the DIP and I think it is perfectly doable without that. This only changes the mangling for C++ only. // C++
int *const *pp;// D
extern(C++) const(int*)* pp; // undefined reference, should not link with C++ variable, different types
You aren't replacing anything. It will work the same as: void foo(T)(const T v) {
writeln( typeof(v).stringof );
}
foo!(void*)( null ); // prints const void*It is really just being expanded to the equivalent: extern(C++) void foo( void * const ); // synonymous with void const(*)
You can't use |
|
Please edit the status field to "Draft". Thanks! |
You can under dlang/dmd#8120 but I think is still doesn't make a difference since you're dealing with structs on the C++ side which we don't have problems with. |
No description provided.