Bug 9053 - g++ confused about ambiguity of overloaded function templates
Summary: g++ confused about ambiguity of overloaded function templates
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.2.1
: P3 normal
Target Milestone: ---
Assignee: Nathan Sidwell
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2002-12-24 10:36 UTC by martin
Modified: 2003-07-25 17:33 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description martin 2002-12-24 10:36:00 UTC
g++ must consider whether return types
of function templates lead to substitution failure
before giving an ambiguity error.  Considering only
arguments is insufficient.

These two function templates

template <class T> typename bar<T>::type foo (T);
template <class T> typename qux<T>::type foo (T);

look ambiguous, but may not be if, when instantiated
with a particular type, one of bar<T> or qux<T>
leads to substitution failure.

Easier to give an example than explain...

The code below compiles under icc and Comeau C++,
but not g++.

(Also: g++ gives ambiguity errors even if foo is never
instantiated, which is probably wrong.)

g++ gives:
ambiguous-template-bug2.cc:10: new declaration `template<class T> qux::type 
   foo(T)'
ambiguous-template-bug2.cc:7: ambiguates old declaration `template<class T> 
   bar::type foo(T)'


source code:

template <typename T> class bar;
template <> struct bar<const char*> { typedef void type; };
template <typename T> class qux;
template <> struct qux<int> { typedef void type; };

template <typename T>
typename bar<T>::type foo (T t) { }

template <typename T>
typename qux<T>::type foo (T t) { }


int
main (int argc, char *argv[])
{
  foo ("foo");
  foo (7);
}

Release:
g++ 3.2.1

Environment:
Linux x86

How-To-Repeat:
Compile source code with g++
Comment 1 Nathan Sidwell 2002-12-24 11:25:21 UTC
Responsible-Changed-From-To: unassigned->nathan
Responsible-Changed-Why: strange,I could've sworn I fixed this.
Comment 2 Nathan Sidwell 2002-12-24 11:25:21 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: confirmed.
Comment 3 Nathan Sidwell 2002-12-24 14:11:07 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: 2002-12-24  Nathan Sidwell  <nathan@codesourcery.com>
    
    	PR c++/9053
    	* decl.c (duplicate_decls): Templates may be disambiguated by
    	return type.
    	
    	PR c++/8702
    	* decl2.c (check_classfn): Use lookup_fnfield_1. List all
    	conversion operators on failure.
Comment 4 Nathan Sidwell 2002-12-24 22:12:17 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/9053
Date: 24 Dec 2002 22:12:17 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	nathan@gcc.gnu.org	2002-12-24 14:12:17
 
 Modified files:
 	gcc/cp         : ChangeLog decl.c decl2.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/lookup: decl1.C decl2.C 
 
 Log message:
 	cp:
 	PR c++/9053
 	* decl.c (duplicate_decls): Templates may be disambiguated by
 	return type.
 	
 	PR c++/8702
 	* decl2.c (check_classfn): Use lookup_fnfield_1. List all
 	conversion operators on failure.
 	testsuite
 	* g++.dg/lookup/decl1.C: New test.
 	* g++.dg/lookup/decl2.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.3089&r2=1.3090
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&r1=1.970&r2=1.971
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl2.c.diff?cvsroot=gcc&r1=1.578&r2=1.579
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2275&r2=1.2276
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/decl1.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/decl2.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
 

Comment 5 Nathan Sidwell 2002-12-24 22:44:43 UTC
From: nathan@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: c++/9053
Date: 24 Dec 2002 22:44:43 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	nathan@gcc.gnu.org	2002-12-24 14:44:43
 
 Modified files:
 	gcc/cp         : ChangeLog decl.c decl2.c cp-tree.h call.c 
 	                 parse.y 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/g++.dg/lookup: decl1.C decl2.C scoped3.C 
 
 Log message:
 	cp:
 	PR C++/7964
 	* cp-tree.h (resolve_scoped_fn_name): Prototype.
 	* call.c (resolve_scoped_fn_name): New function. Deal with
 	more template expansion. Broken out of ...
 	* parse.y (parse_finish_call_expr): ... here. Call it.
 	* decl2.c (build_expr_from_tree, CALL_EXPR): Use
 	resolve_scoped_fn_name and build_call_from_tree.
 	
 	PR c++/9053
 	* decl.c (duplicate_decls): Templates may be disambiguated by
 	return type.
 	
 	PR c++/8702
 	* decl2.c (check_classfn): Use lookup_fnfield_1. List all
 	conversion operators on failure.
 	testsuite
 	* g++.dg/lookup/scoped3.C: New test.
 	
 	* g++.dg/lookup/decl1.C: New test.
 	* g++.dg/lookup/decl2.C: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.7&r2=1.3076.2.8
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.965.2.1&r2=1.965.2.2
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl2.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.575.2.2&r2=1.575.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/cp-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.776.2.2&r2=1.776.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/call.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.341&r2=1.341.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/parse.y.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.284.2.2&r2=1.284.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.7&r2=1.2261.2.8
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/decl1.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/decl2.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/lookup/scoped3.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1