public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [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>.
@ 2007-05-04 13:32 gcc-bugzilla at gcc dot gnu dot org
2007-05-04 23:51 ` [Bug c++/31816] " pinskia at gcc dot gnu dot org
` (9 more replies)
0 siblings, 10 replies; 11+ messages in thread
From: gcc-bugzilla at gcc dot gnu dot org @ 2007-05-04 13:32 UTC (permalink / raw)
To: gcc-bugs
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
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
@ 2007-05-04 23:51 ` pinskia at gcc dot gnu dot org
2007-05-05 15:53 ` nicolas dot burrus at ensta dot fr
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-05-04 23:51 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from pinskia at gcc dot gnu dot org 2007-05-05 00:50 -------
This is what the standard says is to happen.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
2007-05-04 23:51 ` [Bug c++/31816] " pinskia at gcc dot gnu dot org
@ 2007-05-05 15:53 ` nicolas dot burrus at ensta dot fr
2008-06-30 16:32 ` jason dot hoos at syclo dot com
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: nicolas dot burrus at ensta dot fr @ 2007-05-05 15:53 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from nicolas dot burrus at ensta dot fr 2007-05-05 16:53 -------
Indeed, you're right, from the standard, section 14.6.3 (non dependent names)
in the template section : "Non-dependent names used in a template definition
are found using the usual name lookup and bound at the
point they are used".
However, in this case, shouldn't g++ be consistent and still fail for
QSet<int*> if void qHash(double) is not defined before the template definition
?
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
2007-05-04 23:51 ` [Bug c++/31816] " pinskia at gcc dot gnu dot org
2007-05-05 15:53 ` nicolas dot burrus at ensta dot fr
@ 2008-06-30 16:32 ` jason dot hoos at syclo dot com
2010-02-02 20:44 ` gael dot guennebaud at gmail dot com
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: jason dot hoos at syclo dot com @ 2008-06-30 16:32 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from jason dot hoos at syclo dot com 2008-06-30 16:32 -------
(In reply to comment #1)
> Fix:
>
> Do not overload a function before and after a template class
> using it.
>
Another workaround (if rearranging the definitions isn't feasible): before any
of the function definitions, define a template function that calls the
function, and then use that template in other template definitions:
template <class X>
void tmplQHash(X x) { qHash(x); }
void qHash(double) {}
template <class T>
struct QSet
{
void foo()
{
tmplQHash(T());
}
};
void qHash(int*) {}
int main()
{
QSet<double> s;
s.foo(); // ok
QSet<int*> sp;
sp.foo(); // ok now
}
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (2 preceding siblings ...)
2008-06-30 16:32 ` jason dot hoos at syclo dot com
@ 2010-02-02 20:44 ` gael dot guennebaud at gmail dot com
2010-02-02 21:02 ` paolo dot carlini at oracle dot com
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gael dot guennebaud at gmail dot com @ 2010-02-02 20:44 UTC (permalink / raw)
To: gcc-bugs
------- Comment #5 from gael dot guennebaud at gmail dot com 2010-02-02 20:44 -------
I hit the same issue, and according to c++ standard, here we have a _dependent_
lookup, and so the qHash(int*) overloads should be found.
Here is an example directly taken from the C++ standard (14.6, #9):
#include <iostream>
void f(char) {std::cout << "char ";}
template<class T> void g(T t)
{
f(1); // f(char)
f(T(1)); // dependent
f(t); // dependent
}
void f(int) {std::cout << "int ";}
int main()
{
g(2); // will cause one call of f(char) followed
// by two calls of f(int)
g('a'); // will cause three calls of f(char)
}
according to the c++ standard, the output should be:
char int int char char char
but gcc (4.x) returns:
char char char char char char
so, bug or non-bug ?
--
gael dot guennebaud at gmail dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |gael dot guennebaud at gmail
| |dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (3 preceding siblings ...)
2010-02-02 20:44 ` gael dot guennebaud at gmail dot com
@ 2010-02-02 21:02 ` paolo dot carlini at oracle dot com
2010-02-02 21:13 ` paolo dot carlini at oracle dot com
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-02-02 21:02 UTC (permalink / raw)
To: gcc-bugs
------- Comment #6 from paolo dot carlini at oracle dot com 2010-02-02 21:02 -------
Did you check for DRs filed and resolved after C++03? As an additional data
point, the behavior with the current Intel compiler is exactly the same as GCC.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (4 preceding siblings ...)
2010-02-02 21:02 ` paolo dot carlini at oracle dot com
@ 2010-02-02 21:13 ` paolo dot carlini at oracle dot com
2010-02-02 21:26 ` jason at gcc dot gnu dot org
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-02-02 21:13 UTC (permalink / raw)
To: gcc-bugs
------- Comment #7 from paolo dot carlini at oracle dot com 2010-02-02 21:12 -------
Jason, shall we complete the triage of this rather old PR? Thanks in advance
for any help...
--
paolo dot carlini at oracle dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jason at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (6 preceding siblings ...)
2010-02-02 21:26 ` jason at gcc dot gnu dot org
@ 2010-02-02 21:26 ` jason at gcc dot gnu dot org
2010-02-03 20:56 ` [Bug c++/31816] [DR 225] " paolo dot carlini at oracle dot com
2010-02-05 12:52 ` paolo dot carlini at oracle dot com
9 siblings, 0 replies; 11+ messages in thread
From: jason at gcc dot gnu dot org @ 2010-02-02 21:26 UTC (permalink / raw)
To: gcc-bugs
------- Comment #9 from jason at gcc dot gnu dot org 2010-02-02 21:26 -------
Suspending pending resolution of issue 225.
--
jason at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |SUSPENDED
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (5 preceding siblings ...)
2010-02-02 21:13 ` paolo dot carlini at oracle dot com
@ 2010-02-02 21:26 ` jason at gcc dot gnu dot org
2010-02-02 21:26 ` jason at gcc dot gnu dot org
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: jason at gcc dot gnu dot org @ 2010-02-02 21:26 UTC (permalink / raw)
To: gcc-bugs
------- Comment #8 from jason at gcc dot gnu dot org 2010-02-02 21:26 -------
That example does not match the text of the standard, and was fixed by
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#197
However, the standard might change to say what you expect, depending on the
resolution of http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#225
--
jason at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Ever Confirmed|0 |1
Last reconfirmed|0000-00-00 00:00:00 |2010-02-02 21:26:07
date| |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] [DR 225] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (7 preceding siblings ...)
2010-02-02 21:26 ` jason at gcc dot gnu dot org
@ 2010-02-03 20:56 ` paolo dot carlini at oracle dot com
2010-02-05 12:52 ` paolo dot carlini at oracle dot com
9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-02-03 20:56 UTC (permalink / raw)
To: gcc-bugs
------- Comment #10 from paolo dot carlini at oracle dot com 2010-02-03 20:56 -------
Thanks. Jason, I was adding [DR 225] to the Subject when I noticed *another* DR
225, PR29131. Do we want to keep both or not?
--
paolo dot carlini at oracle dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|If a function foo is defined|[DR 225] If a function foo
|before declaring a template |is defined before declaring
|class A<T>, overloaded |a template class A<T>,
|version of foo defined after|overloaded version of foo
|the class declaration will |defined after the class
|not be available within |declaration will not be
|class A<T>. |available within class A<T>.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/31816] [DR 225] 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>.
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
` (8 preceding siblings ...)
2010-02-03 20:56 ` [Bug c++/31816] [DR 225] " paolo dot carlini at oracle dot com
@ 2010-02-05 12:52 ` paolo dot carlini at oracle dot com
9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-02-05 12:52 UTC (permalink / raw)
To: gcc-bugs
------- Comment #11 from paolo dot carlini at oracle dot com 2010-02-05 12:52 -------
Thus I'm resolving this as duplicate, if somebody disagrees, please re-open,
thanks.
*** This bug has been marked as a duplicate of 29131 ***
--
paolo dot carlini at oracle dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|SUSPENDED |RESOLVED
Resolution| |DUPLICATE
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31816
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2010-02-05 12:52 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-04 13:32 [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> gcc-bugzilla at gcc dot gnu dot org
2007-05-04 23:51 ` [Bug c++/31816] " pinskia at gcc dot gnu dot org
2007-05-05 15:53 ` nicolas dot burrus at ensta dot fr
2008-06-30 16:32 ` jason dot hoos at syclo dot com
2010-02-02 20:44 ` gael dot guennebaud at gmail dot com
2010-02-02 21:02 ` paolo dot carlini at oracle dot com
2010-02-02 21:13 ` paolo dot carlini at oracle dot com
2010-02-02 21:26 ` jason at gcc dot gnu dot org
2010-02-02 21:26 ` jason at gcc dot gnu dot org
2010-02-03 20:56 ` [Bug c++/31816] [DR 225] " paolo dot carlini at oracle dot com
2010-02-05 12:52 ` paolo dot carlini at oracle dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).