I use an extern C function that returns dynamically allocated const char*
.
I want to use unique_ptr<const char, decltype(std::free)>
to manage it.
But there is no std::free(const void*)
overload so I get invalid conversion from 'const void*' to 'void*'
and have to use const_cast<char*>()
.
Is this just a Standard Library imperfection or there is something else behind it?
That is it is a constant pointer that points to a const object of type GLvoid . This parameter declaration const GLvoid * const * indices. means that using pointer indices you may not change the pointer (or pointers if this pointer points to the first element of an array of pointers) it points to.
void* is normally used as a way to pass non-specific pointers around (e.g. with memcpy() ). If you want to pass a const char* to such a function then you cannot use void* or you lose the fact that the thing it points to is constant and cannot be altered.
const void is a type which you can form a pointer to. It's similar to a normal void pointer, but conversions work differently. For example, a const int* cannot be implicitly converted to a void* , but it can be implicitly converted to a const void* .
std::free
is inherited from the C standard library. C does not have overloading, so a const
overload could not have been inherited.
While the C++ standard library has extended the inherited C library with some useful overloads, a const
overload has not been added for free
.
Either such an overload has never been considered, or it has not been considered necessary enough to warrant its addition to the standard. I have not come across any openly available proposal for such an addition -- although I haven't read all proposals there have ever been, nor have I attended any committee meetings or workshops, so I cannot deny the possibility of the existence of such a document.
To work around the lack of such an overload, you can indeed use const_cast
. In this context, it is completely safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With