Bug 23513 - [4.0/4.1 Regression] overload resolution fails to select a more specialized template
Summary: [4.0/4.1 Regression] overload resolution fails to select a more specialized t...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P2 normal
Target Milestone: 4.0.3
Assignee: Nathan Sidwell
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2005-08-22 09:38 UTC by Maxim Egorushkin
Modified: 2005-10-05 11:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.4.0
Known to fail: 4.0.2 4.1.0
Last reconfirmed: 2005-09-16 07:30:41


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Maxim Egorushkin 2005-08-22 09:38:24 UTC
This well-formed complete source is rejected by g++, but compiles fine 
with comeau online 4.3.3 BETA August 4, 2003.
A related discussion on clc++ http://groups-beta.google.com/group/comp.
lang.c++/msg/1475f4e8f1ff9982

struct A
{
    template<class T> void operator<<(T&);
};

template<class T> struct B {};
template<class T> void operator<<(A&, B<T>&);

int main()
{
    A a;
    B<A> b;
    a << b;
}

[max@localhost exp]$ g++ -v -save-temps -Wall exp.cpp -o exp
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-
checking=release --with-system-zlib --enable-__cxa_atexit --disable-
libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,
objc,java,f95,ada --enable-java-awt=gtk --with-java-home=/usr/lib/jvm/
java-1.4.2-gcj-1.4.2.0/jre --host=i386-redhat-linux
Thread model: posix
gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
 /usr/libexec/gcc/i386-redhat-linux/4.0.1/cc1plus -E -quiet -v -
D_GNU_SOURCE exp.cpp -Wall -fpch-preprocess -o exp.ii
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../
i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1
 /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/i386-redhat-
linux
 /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/backward
 /usr/local/include
 /usr/lib/gcc/i386-redhat-linux/4.0.1/include
 /usr/include
End of search list.
 /usr/libexec/gcc/i386-redhat-linux/4.0.1/cc1plus -fpreprocessed exp.ii -quiet 
-dumpbase exp.cpp -auxbase exp -Wall -version -o exp.s
GNU C++ version 4.0.1 20050727 (Red Hat 4.0.1-5) (i386-redhat-linux)
	compiled by GNU C version 4.0.1 20050727 (Red Hat 4.0.1-5).
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-
heapsize=64417
exp.cpp: In function 'int main()':
exp.cpp:13: error: ambiguous overload for 'operator<<' in 'a << b'
exp.cpp:3: note: candidates are: void A::operator<<(T&) [with T = B<A>]
exp.cpp:7: note:                 void operator<<(A&, B<T>&) [with T = A]

# 1 "exp.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "exp.cpp"
struct A
{
    template<class T> void operator<<(T&);
};

template<class T> struct B {};
template<class T> void operator<<(A&, B<T>&);

int main()
{
    A a;
    B<A> b;
    a << b;
}
Comment 1 Andrew Pinski 2005-08-26 15:01:12 UTC
It started to fail after 20041211.
Comment 2 Mark Mitchell 2005-08-27 21:49:11 UTC
Nathan --

This change comes from:

2005-04-03  Nathan Sidwell  <nathan@codesourcery.com>

        PR c++/20723
        * pt.c (more_specialized_fn): Member functions are unordered wrt
        non-members.  Conversion operators are unordered wrt other
        functions.

I looked at DR124, but I didn't see anything to justify the "member functions
are unordered wrt non-mebers" claim.  What am I missing?

-- Mark
Comment 3 Mark Mitchell 2005-09-15 21:19:41 UTC
Nathan, do you have any insights about this PR?
Comment 4 Nathan Sidwell 2005-09-16 07:30:41 UTC
I shall take a look
Comment 5 Nathan Sidwell 2005-09-16 13:37:11 UTC
This is a piece of underspecification in the resolution of DR 214
(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#214).  That
basically tells one to compare pairs of arguments.  But in this case we have a
member function and a non-member function -- how should we treat the this
pointer of the member function and the first arg of the non-member function?

As the algorithm did not give any indication as to how to order these, I made
them unordered.

Experimentation using the edg 3.6 frontend I find it to skip the first argument
of both functions (either an explicit arg or implicit this pointer) if either
function is a member function.  The alternative, of comparing the this pointer
arg with the explicit arg of the other function requires treating the this
pointer argument as-if it were a reference to the object (otherwise it'll never
deduce in either direction as the explicit argument must be a by-value or
by-reference object).  Experimentation shows the EDG front end does not take
that approach.
Comment 6 GCC Commits 2005-10-05 09:15:51 UTC
Subject: Bug 23513

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	nathan@gcc.gnu.org	2005-10-05 09:15:47

Modified files:
	gcc/cp         : ChangeLog call.c pt.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.dg/template: spec22.C 
Added files:
	gcc/testsuite/g++.dg/template: spec26.C 

Log message:
	cp:
	PR c++/23513
	* call.c (joust): Adjust length count to more_specialized_fn.
	* pt.c (more_specialized_fn): Cope with non-static member vs
	non-member.
	testsuite:
	PR c++/23513
	* g++.dg/template/spec22.C: Robustify test.
	* g++.dg/template/spec26.C: New.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4911&r2=1.4912
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&r1=1.553&r2=1.554
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&r1=1.1040&r2=1.1041
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.6138&r2=1.6139
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/spec26.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/spec22.C.diff?cvsroot=gcc&r1=1.1&r2=1.2

Comment 7 Nathan Sidwell 2005-10-05 11:07:13 UTC
fixed mainline and 4.0.3
2005-10-05  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/23513
	* call.c (joust): Adjust length count to more_specialized_fn.
	* pt.c (more_specialized_fn): Cope with non-static member vs
	non-member.