This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

error: forming reference to void


Hello!

I've found this so trivial, so I hesitated to send it to "bugs".
Maybe someone can take a look at this, perhaps (and likely) I'm missing
something.

Consider the following (very reduced) class to be used as some sort of
"smart pointer":

template<class T> class ptr
{
private:
	T* val;
public:
	T* operator ->() {return val;}
	T& operator* () {return *val;}
	operator T* () {return val;}
};

Then, just declaring variable "ptr<void> foo;" and _even_not_using_
"operator *()" gives compiler error: "error: forming reference to void".

However, declaring variable "ptr<int> bar;" works fine, what is
inconsistent with previous case, coz "operator ->()" would never work on
"int", anyway.

The question is, why gcc tries to resolve "operator *()" at variable
declaration time, but not at operator invocation time? Yeah, *(void*) is
meaningless, but so is (int)->.

Googling [gcc template "error: forming reference to void"] gives no
meaningful results.

I can't believe that nobody had this problem before. I have a ugly
workaround: At first declare "template<> class ptr<void>;" without
"operator *()", and then generic "template<class T> class ptr;". Common
base class also eliminates code duplication, but introduces more casts,
what is not generally acceptable.

Thank you for Your answers.
__
AZ

PS
gcc version 4.1.2 20070626 (Red Hat 4.1.2-13)



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]