Issue 100869
Summary Local object's destructor executed before function return value evaluation
Labels new issue
Assignees
Reporter fuhsnn
    The code use a lambda class member to implement the scope guard pattern. The object "guard"'s destructor (which execute the lambda) should be run after the return value being determined. 

godbolt: https://godbolt.org/z/aYzcn5dKK
```
#include <cstdio>

template <typename T>
struct scopeguard {
    T lambda;
    scopeguard(T&& init) : lambda(init) {};
    ~scopeguard() { lambda(); }
};

struct S {
    int i;
};

S test(void) {
    S s {};
    scopeguard guard { [&](){ s.i += 7; } };

 std::printf("returning %d\n", s.i = 22);
    return s;
}

int main()
{
  std::printf("returned  %d\n", test().i);
  return 0;
}
```
GCC and MSVC print:
```
returning 22
returned 22
```
Clang print:
```
returning 22
returned  29
```

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to