User account creation filtered due to spam.

Bug 16246 - [3.3/3.4/4.0 regression] Incorrect template argument deduction
Summary: [3.3/3.4/4.0 regression] Incorrect template argument deduction
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 3.4.2
Assignee: Mark Mitchell
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2004-06-28 14:18 UTC by Vasili Burdo
Modified: 2004-09-13 14:15 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 2.95.3
Known to fail: 4.0 3.4.0 3.3.3 3.2.3 3.0.4
Last reconfirmed: 2004-06-28 16:25:12


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vasili Burdo 2004-06-28 14:18:59 UTC
the code:
-------------------------------------
//#include <algorithm>
namespace std
{
	template<class T> T min(T const&,T const&);
		//declaring min() instead of including <algorithm> to save 
space.
		//this example works in both cases
}

template<unsigned N> 
struct tester
{
	template<class T,unsigned M> 
	void fn( T(&val)[M] )
	{
		std::min(N,M);
		std::min(M,N);
	}
};

int main()
{
	tester<10> t;
	t.fn("1234");
}
-------------------------------------
fails on mingw GCC-3.4.0 with diagnostics:
-------------------------------------
bug2.cxx: In member function `void tester<N>::fn(T (&)[M]) [with T = const 
char, unsigned int M = 5, unsigned int N = 10u]':
bug2.cxx:20:   instantiated from here
bug2.cxx:12: error: no matching function for call to `min(unsigned int, int)'
bug2.cxx:13: error: no matching function for call to `min(int, unsigned int)'
-------------------------------------
"M" deduced to be "int", but it declared as "unsigned" in "template<>" clause
Comment 1 Andrew Pinski 2004-06-28 16:25:12 UTC
Confirmed.
Comment 2 Wolfgang Bangerth 2004-06-28 17:09:11 UTC
Confirmed indeed. This is a rather peculiar failure: 
---------------- 
template <typename T> void foo (T, T); 
 
template <unsigned N, unsigned M>  
int bar( const char(&val)[M] ) 
{ 
  foo (N,M); 
} 
 
int i = bar<10>("1234"); 
---------------- 
 
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc 
x.cc: In function `int bar(const char (&)[M]) [with unsigned int N = 10u, 
unsigned int M = 5]': 
x.cc:9:   instantiated from here 
x.cc:6: error: no matching function for call to `foo(unsigned int, int)' 
 
Note that both N and M are declared to be of type unsigned. However, only 
N is explicitly specified and M is deduced. From the error message, we 
see that M is assumed to be unsigned, but the value given in the template 
parameter list doesn't have the 'u' suffix, so it looks like as if gcc 
knows that the type is unsigned, but that the node in which the actual 
value is stored is a signed integer. This mismatch then causes the failure 
of not finding an instance of foo(). 
 
This used to work in 2.95, so is a regression. 
 
W. 
Comment 3 Mark Mitchell 2004-08-18 01:48:49 UTC
Working on a fix.
Comment 4 CVS Commits 2004-08-18 02:55:18 UTC
Subject: Bug 16246

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2004-08-18 02:55:14

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

Log message:
	PR c++/16246
	* pt.c (unify): Tidy ARRAY_TYPE handling.  Make sure that non-type
	arguments have the same type as the corresponding parameter.
	
	PR c++/16246
	* g++.dg/template/array7.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4280&r2=1.4281
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&r1=1.906&r2=1.907
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4153&r2=1.4154
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/array7.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 5 CVS Commits 2004-08-18 03:00:04 UTC
Subject: Bug 16246

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	mmitchel@gcc.gnu.org	2004-08-18 02:59:58

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

Log message:
	PR c++/16246
	* pt.c (unify): Tidy ARRAY_TYPE handling.  Make sure that non-type
	arguments have the same type as the corresponding parameter.
	
	PR c++/16246
	* g++.dg/template/array7.C: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3892.2.146&r2=1.3892.2.147
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/pt.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.816.2.38&r2=1.816.2.39
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3389.2.249&r2=1.3389.2.250
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/template/array7.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1

Comment 6 Mark Mitchell 2004-08-18 03:00:35 UTC
Fixed in GCC 3.4.2.
Comment 7 Gabriel Dos Reis 2004-08-29 14:54:51 UTC
Subject: Re:  [3.3/3.4/3.5 regression] Incorrect template argument deduction

"cvs-commit at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| Subject: Bug 16246
| 
| CVSROOT:	/cvs/gcc
| Module name:	gcc
| Branch: 	gcc-3_4-branch
| Changes by:	mmitchel@gcc.gnu.org	2004-08-18 02:59:58

Will not fix this in 3.3.x series -- even though we have a patch for
3.4.x. 

-- Gaby