Summary: | function template can call other function with incorrect parameters | ||
---|---|---|---|
Product: | gcc | Reporter: | Jens Maurer <jens.maurer> |
Component: | c++ | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | bangerth, fang, gcc-bugs, gianni, jajarvi, japple, jason, nathan, pinskia |
Priority: | P3 | Keywords: | accepts-invalid |
Version: | 3.0.1 | ||
Target Milestone: | --- | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | 4.1.0 | Last reconfirmed: | 2005-12-11 21:49:13 |
Attachments: | 4205.ii |
Description
Jens Maurer
2001-09-02 10:16:01 UTC
State-Changed-From-To: open->analyzed State-Changed-Why: confirmed as a bug. I've attached a smaller test case From: Wolfgang Bangerth <bangerth@ticam.utexas.edu> To: gcc-bugs@gcc.gnu.org, <gcc-gnats@gcc.gnu.org>, <nathan@codesourcery.com>, <gdr@codesourcery.com> Cc: Subject: Re: c++/4205: function template can call other function with incorrect parameters Date: Fri, 15 Nov 2002 19:55:02 -0600 (CST) This code compiles silently: ------------------------------------- template<typename F> void quirk(F f) { (*f) (1); } void foo(int i, int j = 5){} void bar(int i, int j) {} int main() { quirk(&foo); quirk(&bar); } ------------------------------------- The assertion of the submitter is that the first quirk(&foo) is ok, since the call to (*f)(1) will substitute the second arg of foo by the default argument of that function. The second call would be wrong. It succeeds, of course, since the function quirk for exactly this template arg has already been compiled, and no re-compilation means no re-check. However, I believe that already the first one is bogus. The template argument F of quirk is void (*) (int, int), so the call to (*p)(1) should be invalid. We should not know about default arguments in quirk, right? I'm surprised that default arguments are propagated to the template function. They don't appear in the type of quirk as well, since if I enter a std::cout << __PRETTY_FUNCTION__ << std::endl; into that function, I only get void quirk(F) [with F = void (*)(int, int)] But that may of course have other roots. Regards Wolfgang ------------------------------------------------------------------------- Wolfgang Bangerth email: bangerth@ticam.utexas.edu www: http://www.ticam.utexas.edu/~bangerth From: Nathanael Nerode <neroden@twcny.rr.com> To: gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, Jens.Maurer@gmx.net, gcc-bugs@gcc.gnu.org, nobody@gcc.gnu.org Cc: Subject: Re: c++/4205 Date: Fri, 3 Jan 2003 07:07:54 -0500 This has a further perverse piece of behavior not noted before: the following code will, correctly, give an error! -- template<typename F> void quirk(F f) { (*f) (1); } // void foo(int i, int j = 5){} void bar(int i, int j) {} int main() { // quirk(&foo); quirk(&bar); } -- And so will this: -- template<typename F> void quirk(F f) { (*f) (1); } void foo(int i, int j = 5){} void bar(int i, int j) {} int main() { quirk(&bar); quirk(&foo); } -- As long as "foo" is seen *first*, it 'immunizes' all subsequent functions. But not otherwise. This is really bad behavior. It may possibly be related to c++/3784, which has similar "first one seen controls everyone else" behavior, though I doubt it. *** Bug 13500 has been marked as a duplicate of this bug. *** *** Bug 4908 has been marked as a duplicate of this bug. *** *** Bug 13916 has been marked as a duplicate of this bug. *** template<typename T, typename U = int> struct foo {}; template<template <typename> class TT> struct bar {}; bar<foo> x; compiles for me with 3.3.2. Is this bug related? Subject: Re: function template can call other function with incorrect parameters "japple at freeshell dot org" <gcc-bugzilla@gcc.gnu.org> writes: | template<typename T, typename U = int> | struct foo {}; | template<template <typename> class TT> | struct bar {}; | bar<foo> x; | | compiles for me with 3.3.2. Is this bug related? Not really. But yes, you have a point: GCC currently does not have a uniform infrastructure for sharing codes for functions and templates. -- Gaby (In reply to comment #8) > Not really. But yes, you have a point: GCC currently does not have > a uniform infrastructure for sharing codes for functions and > templates. See new bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13966 Jim Subject: Re: function template can call other function with incorrect parameters "japple at freeshell dot org" <gcc-bugzilla@gcc.gnu.org> writes: | ------- Additional Comments From japple at freeshell dot org 2004-02-01 22:42 ------- | (In reply to comment #8) | | > Not really. But yes, you have a point: GCC currently does not have | > a uniform infrastructure for sharing codes for functions and | > templates. | | See new bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13966 Thanks. I should have been clearer. Technically, this is a GNU extension that really goes against the C++ committee clarification. (I don't have the DR number off hand). There is a special code in cp/pt.c to implement that extension. I remember having submitted a patch to remove it; but at the time, some people think we should keep it around. I don't necessarily agree. -- Gaby EDG accepts this too. *** Bug 32993 has been marked as a duplicate of this bug. *** This seems like a serious bug and it has been around for 6 years and there has been a patch to fix this as noted by Gaby. Is someone of the opinion that this should not be fixed ? This seems to have been fixed sometime in the 4.3 release cycle; it works properly with the current 4.3, 4.4 and 4.5 branches. |