Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No compiler diagnostic when identifier in a simple-capture appears as the declarator-id of a parameter

Tags:

The section on lambda captures ([expr.prim.lambda.capture]/5) states that

If an identifier in a simple-capture appears as the declarator-id of a parameter of the lambda-declarator's parameter-declaration-clause, the program is ill-formed.

Consider the follow example:

#include <iostream>  int main () {     auto foo = 1234;     auto bar = [foo](int foo) { std::cout << foo << '\n'; };     bar(4321);      } 

The latest GCC version (8.2.0 - released on July 26, 2018) has no diagnostic for this. Neither does latest Clang version (7.0.0 - released on Sep 19, 2018).

Should there be a diagnostic (error/warning) from these compilers (as mentioned in the reference) along the lines of:

// parameter and simple-capture have the same name 

Godbolt Demo here

like image 464
P.W Avatar asked Oct 23 '18 11:10

P.W


1 Answers

That wording was added to C++17 to resolve CWG Defect 2211. It didn't exist in C++14, and it would seem that Clang and GCC haven't caught up to this change up to the versions you are checking.

It's worth noting that the GCC trunk does indeed diagnose that program as ill-formed under C++17.

like image 119
StoryTeller - Unslander Monica Avatar answered Sep 20 '22 22:09

StoryTeller - Unslander Monica