This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/31816] New: 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>.
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 4 May 2007 13:31:52 -0000
- Subject: [Bug c++/31816] New: 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>.
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
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.
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
How-To-Repeat:
Compile the given code.
------- Comment #1 from nicolas dot burrus at ensta dot fr 2007-05-04 14:31 -------
Fix:
Do not overload a function before and after a template class
using it.
--
Summary: 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>.
Product: gcc
Version: 4.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: nicolas dot burrus at ensta dot fr
GCC build triplet: i486-pc-linux-gnu
GCC host triplet: i486-pc-linux-gnu
GCC target triplet: i486-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816