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

Function overloading before and after a template class



>Submitter-Id:	net
>Originator:	Nicolas Burrus
>Organization:	Ensta 
>Confidential:	no
>Synopsis:	If a function foo is defined before declaring a template class A<T>, overloaded version of foo defined after the class declaration will not be available within class A<T>.
>Severity:	serious
>Priority:	medium
>Category:	c++
>Class:		rejects-legal
>Release:	gcc-4.1 (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
>Environment:
System: Linux stageuei9 2.6.18-3-686 #1 SMP Mon Dec 4 16:41:14 UTC 2006 i686 GNU/Linux
Architecture: i686

	
host: i486-pc-linux-gnu
build: i486-pc-linux-gnu
target: i486-pc-linux-gnu
configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-tune=i686 --enable-checking=release i486-linux-gnu
>Description:

Let us consider the following code:
-----
void qHash(double) {}

template <class T>
struct QSet
{
  void foo()
  {
    qHash(T());
  }
};

// void qHash(double) {} // ok if placed here
void qHash(int*) {}

int main()
{
  QSet<double> s;
  s.foo(); // ok

  QSet<int*> sp;
  sp.foo(); // do not compile, qHash(int*) is not considered.
}
-----

It does not compile, because the overloaded version of qHash() for
int* is not available in QSet, even if QSet is instantiated after the
declaration of qHash(int*). If no qHash function is defined before the
declaration of QSet, it works fine. Although I'm not absolutely
certain that this code is valid, my intuition tells me this behavior
is strongly suspect ..

It works fine with g++ 4.0.4, 3.3 and 2.95.

>How-To-Repeat:

Compile the given code.

>Fix:

Do not overload a function before and after a template class
using it.



Reply to: