Issue 97256
Summary Clang uses old closure type when using as default argument instead of using new closure type each time
Labels clang
Assignees
Reporter ranaanoop
    The following program output `12` instead of `11`. The output `11` is expected because as per [dcl.fct.default] "default argument is evaluated each time the function is called with no argument for the corresponding parameter" and as per [expr.prim.lambda.closure]  the type of a lambda _expression_ is unique each time. [Demo](https://godbolt.org/z/fM7Yxx7oz).

```
#include <iostream>

int foo(int x = [](){ static int x = 0; return ++x; }()) { return x; };

int main() {
    std::cout << foo() << foo(); // 12
}
```

Note that at [cppinsights](https://cppinsights.io/s/abca4605), clang does create a new class type each time but still produces the output `12` instead of `11`. So creating a new class type each time but still using the old one seems odd/wrong. Gcc also produces the output `12`. 

The output should be `11` afaik as a new class-type is created each time.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to