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

  • A+
Category:Languages

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);      } 

GCC 8.2.0 has no diagnostic for this. Neither does Clang 7.0.0.

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

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: