www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const void* when wrapping C++

C/C++ sometimes uses a declaration such as 'cost void*', which is a
pointer to const void. D doesn't have an equivalent to this, since
using const(void)* is illegal.

The problem is if there are two overloads in a C++ class such as this:
foo( const void* )   // equivalent to const(void)*, illegal in D
foo( void * const )  // equivalent to const(void*), ok in D

then this becomes hard to recreate on the D side. It's not easy to
simulate it with a template either, since 'void' can't be passed to a
template as a type:

foo( Const!(void)* )  // clever try but won't work, can't pass 'void'
to a template

Some other template tricks might be used but it could really
complicate the user code.

SWIG for example by default ignores one of the methods. The
alternative is to rename one of the methods on the D side, but this
hardly fixes anything since now the user has to figure out which
method to call as the two methods end up having the same type
signature (void*).

So the question is why is const(void)* illegal in D? It could have
some helpful semantics, e.g. the compiler could limit type casts:

const(void)* ptr;
const(int)* intPtr = cast(const(int)*)ptr;  // ok
int* intPtr = cast(int*)ptr;  // not ok

And it would make wrapping C/C++ easier.
Sep 15 2012