Reputation: 366
I am trying to create a QScopedPointer of void with a custom deleter to properly managed resources coming from a C library (which does only supply void* to its internal data structures). However, when I try to instantiate a QScopedPointer of void, I get this error:
/Library/Frameworks/QtCore.framework/Headers/qscopedpointer.h:104:14:
error: cannot form a reference to 'void'
inline T &operator*() const
(from Clang)
or this:
/Library/Frameworks/QtCore.framework/Headers/qscopedpointer.h:104:
error: forming reference to void
(from GCC)
I thought to specialize QScopedPointer or to write a basic scoped pointer with custom deleter, but in both cases I end up duplicated exactly the same code that I have in QScopedPointer.
Is there any way I can get rid (or specialize) that particular function so that the compiler does not complain anymore?
Upvotes: 3
Views: 1664
Reputation: 40849
No. There is no way to write that function such that it won't blow the compiler's mind out. You should ask yourself what sense it would make anyway. How can a smart pointer to void do something inherently impossible to do with a void*?
void * vptr; *vptr = ??; ?? = *vptr; f(*vptr);
None of those statements make any sense at all.
The void* type is a very special type meant to provide opaque typing in C. You can use it in C++ but usually you don't want to. I have a feeling that whatever you're trying to do, there's a better way.
If you really need an opaque pointer type that is smart, you'll have to make it and you'll have to ommit dereferencing functionality.
What kind of surprises me is that C++ compilers are required to not compile member functions of templates that are not used. This would indicate to me that you are actually trying to dereference a void pointer. You can't do that. If you want the pointer out of a smart pointer you use get(). I'm sure QScopedPointer or whatever has a similar function.
Upvotes: 1