Bug 67017

Summary: [DR1265] Mixing init-declarator for variables and functions in declaration with auto type-specifier
Product: gcc Reporter: Anders Granlund <anders.granlund.0>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: webrown.cpp
Priority: P3 Keywords: accepts-invalid
Version: unknown   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2015-07-26 00:00:00

Description Anders Granlund 2015-07-26 13:54:18 UTC
Consider the following program (prog.cc):

  auto i = 0, f();
  int main() {}

Compile it with the following command line:

  g++ prog.cc -std=c++14 -pedantic-errors

No error messages are given. I expect to get at least one error message since the program is ill-formed.

The program is ill-formed by [decl.spec.auto]/8:

  http://eel.is/c++draft/dcl.dcl#dcl.spec.auto-8

  "If the init-declarator-list contains more than one init-declarator, they shall all form declarations of variables."

I have tried this with gcc HEAD 6.0.0 20150725 here:

  http://melpon.org/wandbox/permlink/UaCnMgfMlG9nDqf3
Comment 1 Jonathan Wakely 2015-07-26 20:48:15 UTC
EDG and Clang also accept this in C++14 mode (and like GCC, reject it in C++11 mode).
Comment 2 Anders Granlund 2015-07-26 21:07:48 UTC
(In reply to Jonathan Wakely from comment #1)
> EDG and Clang also accept this in C++14 mode (and like GCC, reject it in
> C++11 mode).

I think that is because type deduction for return types of functions was introduced in c++14.

The following program is well-formed in c++14, but not in c++11:

auto f();
int main() {}
Comment 3 Anders Granlund 2015-07-26 21:41:20 UTC
(In reply to Jonathan Wakely from comment #1)
> EDG and Clang also accept this in C++14 mode (and like GCC, reject it in
> C++11 mode).

(In reply to Anders Granlund from comment #2)
> (In reply to Jonathan Wakely from comment #1)
> > EDG and Clang also accept this in C++14 mode (and like GCC, reject it in
> > C++11 mode).
> 
> I think that is because type deduction for return types of functions was
> introduced in c++14.
> 
> The following program is well-formed in c++14, but not in c++11:
> 
> auto f();
> int main() {}

(In reply to Jonathan Wakely from comment #1)
> EDG and Clang also accept this in C++14 mode (and like GCC, reject it in
> C++11 mode).

It is interesting that the compilers seem to agree, looks like they all have this bug. 

I noticed an interesting thing however. Consider this program:

auto i = 0, f();
auto f() { return false; }
int main() {}

Clang accepts it, but GCC rejects it (I didn't try EDG).
Comment 4 Jonathan Wakely 2015-07-26 23:17:28 UTC
(In reply to Anders Granlund from comment #3)
> > I think that is because type deduction for return types of functions was
> > introduced in c++14.

Yes, I understand why they reject it in C++11 mode, my point was that they all consistently accept it in C++14 mode.

> I noticed an interesting thing however. Consider this program:
> 
> auto i = 0, f();
> auto f() { return false; }
> int main() {}
> 
> Clang accepts it, but GCC rejects it (I didn't try EDG).

EDG accepts that too.
Comment 5 Anders Granlund 2015-07-27 00:05:44 UTC
(In reply to Jonathan Wakely from comment #4)
> (In reply to Anders Granlund from comment #3)
> > > I think that is because type deduction for return types of functions was
> > > introduced in c++14.
> 
> Yes, I understand why they reject it in C++11 mode, my point was that they
> all consistently accept it in C++14 mode.
> 
> > I noticed an interesting thing however. Consider this program:
> > 
> > auto i = 0, f();
> > auto f() { return false; }
> > int main() {}
> > 
> > Clang accepts it, but GCC rejects it (I didn't try EDG).
> 
> EDG accepts that too.

Yes, it is interesting that three different compilers seems to disagree with my interpretation of the c++ standard. I still think this is a bug.

I'm trying to get a second option about this interpretation of the c++ standard here:

https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/_xOC2ou49ZQ
Comment 6 Anders Granlund 2015-07-27 11:14:06 UTC
Now I got the reply. It seems like the wording of the standard changed with the resolution of this c++ standard core defect:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1265

So the problem is that GCC hasn't implemented this yet.
Comment 7 Marek Polacek 2019-07-06 15:00:52 UTC
Resolved in r244071.