error: forming reference to void

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux