I stumbled upon this strange behaviour.
#include <iostream>
#include <functional>
struct adder {
auto add(int x, int y) -> int {
return x + y;
}
};
auto main() -> int {
std::cout << std::invoke(&adder::add, *static_cast<adder*>(nullptr), 1, 2) << '\n';
return 0;
}
It was compiled with gcc version 11.3.0
on Ubuntu Ubuntu1 22.04
When i run it, it returns 3
.
I understand why it is possible since adder
, at this point, is more or less just like a namespace. And when I add some state to the struct I get a segmentation fault
like expected.
Is this part of the standart or maybe just a gcc
thing?