Bug 54485

Summary: g++ should diagnose default arguments in out-of-line definitions for template class member functions
Product: gcc Reporter: Diego Novillo <dnovillo>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler, jason, thiago
Priority: P3 Keywords: accepts-invalid
Version: 4.8.0   
Target Milestone: 4.9.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2012-09-05 00:00:00

Description Diego Novillo 2012-09-04 21:27:27 UTC
In PR 54484, bootstraps were broken with clang++.  The template class vec_t had a member function with default arguments, but the default values were specified in the function definition, instead of its declaration.

According to 8.3.6, that's incorrect: "Default arguments for a member function of a class template shall be specified on the initial declaration of the member function within the class template."

$ cat a.cc
template<typename T>
class K
{
  int fn(int, int);
};

template<typename T>
int K<T>::fn (int a, int b = 3)
{
  return a - b;
}

$ g++ -c a.cc

$ clang++ -c a.cc
a.cc:8:26: error: default arguments cannot be added to an out-of-line definition      of a member of a class template
int K<T>::fn (int a, int b = 3)
                         ^   ~
1 error generated.

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang++ --version
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)Target: x86_64-pc-linux-gnu
Thread model: posix
Comment 1 Thiago Macieira 2012-09-05 07:27:08 UTC
FYI

$ icpc -c a.cc
Comment 2 Richard Biener 2012-09-05 09:20:27 UTC
Confirmed.  Probably worth accepting with -fpermissive?
Comment 3 Paolo Carlini 2013-08-24 02:07:14 UTC
Mine.
Comment 4 Paolo Carlini 2013-10-28 13:59:22 UTC
Patch here: http://gcc.gnu.org/ml/gcc-patches/2013-08/msg01435.html
Comment 5 paolo@gcc.gnu.org 2013-11-25 22:25:02 UTC
Author: paolo
Date: Mon Nov 25 22:24:58 2013
New Revision: 205367

URL: http://gcc.gnu.org/viewcvs?rev=205367&root=gcc&view=rev
Log:
/cp
2013-11-25  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54485
	* decl.c (duplicate_decls): Enforce 8.3.6/6 about default arguments
	for member functions of class templates.

/testsuite
2013-11-25  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54485
	* g++.dg/other/default8.C: New.
	* g++.dg/tc1/dr217.C: Remove xfail.
	* g++.dg/other/default5.C: Adjust.
	* g++.old-deja/g++.mike/p1989.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/other/default8.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/other/default5.C
    trunk/gcc/testsuite/g++.dg/tc1/dr217.C
    trunk/gcc/testsuite/g++.old-deja/g++.mike/p1989.C
Comment 6 Paolo Carlini 2013-11-25 22:26:14 UTC
Fixed for 4.9.0.