This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
error: forming reference to void
- From: Arturs Zoldners <az at tuci dot lv>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 24 Sep 2007 19:48:27 +0300
- Subject: error: forming reference to void
- Reply-to: az at tuci dot lv
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)