Open
Description
The code use the [[gnu::cleanup()]]
attribute to register a cleanup function. The cleanup function should be called after the return value being determined.
godbolt link: https://godbolt.org/z/zY5abr361
#include <stdio.h>
typedef struct {
int i;
} S;
void inc(S *obj) {
obj->i += 7;
}
S test(void) {
S s [[gnu::cleanup(inc)]];
printf("returning %d\n", s.i = 22);
return s;
}
int main()
{
printf("returned %d\n", test().i);
return 0;
}
GCC print:
returning 22
returned 22
Clang print:
returning 22
returned 29