Bug 15339 - [DR217] Adding default arguments to function templates in redeclarations should be forbidden
Summary: [DR217] Adding default arguments to function templates in redeclarations shou...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.3
: P2 normal
Target Milestone: 5.0
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 11540 28179 48372 61471 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-05-07 15:02 UTC by MichaelLi
Modified: 2017-10-16 10:39 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 3.4.0, 4.0.0
Last reconfirmed: 2005-07-06 01:50:44


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description MichaelLi 2004-05-07 15:03:00 UTC
//*******mytest.hpp******* 
#ifndef MYTEST_H 
#define MYTEST_H 
#include <iostream> 
#include <string> 
template<typename T>    //declaration 
void fun(T val, const std::string seperator); //error, change to 
                                         //seperator = " " , then it works well

template<typename T>    //definition 
void fun(T val, const std::string seperator = " ") 
{ 
    std::cout<<val<<seperator<<std::endl; 
} 
#endif 

//***********test.cpp********* 
#include "mytest.hpp" 
int main() 
{ 
   fun(10); 
}

g++ test.cpp -o test
and reads "test.cpp:4:error: no matching function for call to `fun(int)'".
I think the function should be resolved from its definition not its 
declaration the first one it encountered. It means that although no default 
argument is provided in function declaration, as long as it has one default in 
the definition, it should also work well.
Comment 1 Wolfgang Bangerth 2004-05-07 15:30:45 UTC
Confirmed, here is a smaller testcase: 
----------- 
template<typename> void fun(int); 
template<typename> void fun(int = 0) {}  
 
int main()  
{ fun(); } 
----------- 
 
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc 
x.cc: In function `int main()': 
x.cc:10: error: no matching function for call to `fun()' 
 
However, we get this from icc: 
g/x> icc -Xc -ansi x.cc 
x.cc(2): warning #845: specifying a default argument when redeclaring an 
unreferenced function template is nonstandard 
  template<typename> void fun(int = 0) {}  
                          ^ 
 
x.cc(5): error: no instance of function template "fun" matches the argument 
list 
  { fun(); } 
    ^ 
 
compilation aborted for x.cc (code 2) 
 
 
So it seems that this is wrong code. Someone will have to look up the exact 
text in the standard though. I find it odd that this only applies to  
templates, not to non-template functions. 
 
W. 
Comment 2 Wolfgang Bangerth 2004-05-07 15:37:19 UTC
Actually, that was easy. 8.3.6/4 specificly says 
  For non-template functions, default arguments can be added in later 
  declarations. 
 
So the code is invalid. 
 
W. 
Comment 3 Gabriel Dos Reis 2004-05-07 16:24:19 UTC
Subject: Re:  function call error

"bangerth at dealii dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| Actually, that was easy. 8.3.6/4 specificly says 
|   For non-template functions, default arguments can be added in later 
|   declarations. 
|  
| So the code is invalid. 

Yes, you're right.  But we should be diagnosing the declaration
in the first place -- which we fail.  So there is an issue here, but
not that one the submitter thought about.

-- Gaby
Comment 4 llewelly 2004-05-07 16:36:26 UTC
Subject: Re:  function call error

"bangerth at dealii dot org" <gcc-bugzilla@gcc.gnu.org> writes:

> ------- Additional Comments From bangerth at dealii dot org  2004-05-07 15:30 -------
> Confirmed, here is a smaller testcase: 
> ----------- 
> template<typename> void fun(int); 
> template<typename> void fun(int = 0) {}  
>  
> int main()  
> { fun(); } 
> ----------- 
>  
> g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc 
> x.cc: In function `int main()': 
> x.cc:10: error: no matching function for call to `fun()' 
>  
> However, we get this from icc: 
> g/x> icc -Xc -ansi x.cc 
> x.cc(2): warning #845: specifying a default argument when redeclaring an 
> unreferenced function template is nonstandard 
>   template<typename> void fun(int = 0) {}  
>                           ^ 
>  
> x.cc(5): error: no instance of function template "fun" matches the argument 
> list 
>   { fun(); } 
>     ^ 
>  
> compilation aborted for x.cc (code 2) 
>  
>  
> So it seems that this is wrong code. Someone will have to look up the exact 
> text in the standard though. I find it odd that this only applies to  
> templates, not to non-template functions.

It's in 8.3.6/4 . But see issue 226:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#226

Basicly, it's a leftover from when there was no way to explicitly
    specifiy template arguments for function templates. 
Comment 5 Wolfgang Bangerth 2004-05-07 16:54:05 UTC
I fail to see the relevance of the DR to the present case. It doesn't 
say anything about default arguments for repeated declarations, does 
it? 
 
W. 
Comment 6 Giovanni Bajo 2004-05-08 16:29:03 UTC
Reopened before there is an issue
Comment 7 Giovanni Bajo 2004-05-08 16:32:57 UTC
This should be diagnosed:

------------------------------------
template<typename> void fun(int); 
template<typename> void fun(int = 0) {}  
------------------------------------

but it's not. I don't think it's a regression, but can anybody check?

Also, DR217 (TC1 status) says that this is also for regular member function of 
class templates. In other words, the following is invalid as well:

------------------------------------
    template <class T>
    struct S
    {
	void foo (int);
    };

    template <class T>
    void S<T>::foo (int = 0) { }
------------------------------------
(note that this testcase is already in our testsuite: g++.dg/tc1/dr217.C)
Comment 8 MichaelLi 2004-05-09 14:17:55 UTC
Subject: RE:  [DR217] Adding default arguments to function templates in
 redeclartions should be forbidden

I could not find "g++.dg/tc1/dr217.C" in the testsuite of the recently 
released package gcc-3.4.0.tar.bz2. Where could I get it? Thanks.


>From: "giovannibajo at libero dot it" <gcc-bugzilla@gcc.gnu.org>
>Reply-To: gcc-bugzilla@gcc.gnu.org
>To: MichaelLi8195@hotmail.com
>Subject: [Bug c++/15339] [DR217] Adding default arguments to function 
templates in redeclartions should be forbidden
>Date: 8 May 2004 16:32:58 -0000
>
>------- Additional Comments From giovannibajo at libero dot it  2004-05-08 
16:32 -------
>This should be diagnosed:
>
>------------------------------------
>template<typename> void fun(int);
>template<typename> void fun(int = 0) {}
>------------------------------------
>
>but it's not. I don't think it's a regression, but can anybody check?
>
>Also, DR217 (TC1 status) says that this is also for regular member 
function of
>class templates. In other words, the following is invalid as well:
>
>------------------------------------
>     template <class T>
>     struct S
>     {
>	void foo (int);
>     };
>
>     template <class T>
>     void S<T>::foo (int = 0) { }
>------------------------------------
>(note that this testcase is already in our testsuite: g++.dg/tc1/dr217.C)
>
>
>--
>            What    |Removed                     |Added
>----------------------------------------------------------------------------

>            Keywords|                            |accepts-invalid
>       Known to fail|                            |3.4.0 3.5.0
>             Summary|function call error         |[DR217] Adding default
>                    |                            |arguments to function
>                    |                            |templates in 
redeclartions
>                    |                            |should be forbidden
>
>
>http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15339
>
>------- You are receiving this mail because: -------
>You reported the bug, or are watching the reporter.


Comment 9 Wolfgang Bangerth 2004-05-09 15:18:22 UTC
No, it's not a regression -- we never detected this at least back to 
2.95. 
 
With respect to the testcase: they only seem to be in the CVS archive 
since after the 3.4 branch was created. Giovanni added these files on 
2004/02/17. They are not present on the 3.4 branch. 
 
W. 
 
Comment 10 Giovanni Bajo 2004-07-13 20:13:27 UTC
*** Bug 11540 has been marked as a duplicate of this bug. ***
Comment 11 Giovanni Bajo 2004-07-13 20:14:41 UTC
PR 11827 contains a small patch for this bug by Jakub, but it doesn't look OK 
(it wait for instantiation).
Comment 12 Andrew Pinski 2006-06-27 14:47:36 UTC
*** Bug 28179 has been marked as a duplicate of this bug. ***
Comment 13 Jakub Jelinek 2010-02-12 19:56:52 UTC
default3.C g4 test has been XFAILed, for details see
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43033#c3
Comment 14 Paolo Carlini 2011-10-12 16:11:22 UTC
*** Bug 48372 has been marked as a duplicate of this bug. ***
Comment 15 Paolo Carlini 2014-07-31 17:36:37 UTC
Mine.
Comment 16 paolo@gcc.gnu.org 2014-08-02 19:42:04 UTC
Author: paolo
Date: Sat Aug  2 19:41:32 2014
New Revision: 213519

URL: https://gcc.gnu.org/viewcvs?rev=213519&root=gcc&view=rev
Log:
/cp
2014-08-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/15339
	* decl.c (check_redeclaration_no_default_args): New.
	(duplicate_decls): Use it, handle default arguments
	in redeclarations of function templates.

/testsuite
2014-08-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/15339
	* g++.dg/other/default9.C: New.
	* g++.dg/other/default10.C: Likewise.
	* g++.dg/other/default3.C: Remove xfail.

Added:
    trunk/gcc/testsuite/g++.dg/other/default10.C
    trunk/gcc/testsuite/g++.dg/other/default9.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/other/default3.C
Comment 17 Paolo Carlini 2014-08-02 19:42:50 UTC
Fixed.
Comment 18 paolo@gcc.gnu.org 2014-08-02 21:39:06 UTC
Author: paolo
Date: Sat Aug  2 21:38:34 2014
New Revision: 213520

URL: https://gcc.gnu.org/viewcvs?rev=213520&root=gcc&view=rev
Log:
2014-08-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/15339
	* testsuite/26_numerics/headers/complex/synopsis.cc: Fix.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/testsuite/26_numerics/headers/complex/synopsis.cc
Comment 19 Paolo Carlini 2017-10-16 10:39:32 UTC
*** Bug 61471 has been marked as a duplicate of this bug. ***