The silly question. Could anyone please explain to me how this code works? (from here https://alexpolt.github.io/type-loophole.html)
#include <string>
#include <type_traits>
template<int N> struct tag{};
template<typename T, int N>
struct loophole_t {
friend auto loophole(tag<N>) { return T{}; };
};
auto loophole(tag<0>);
int main() {
sizeof( loophole_t<std::string, 0> );
static_assert(std::is_same< std::string, decltype( loophole(tag<0>{}) ) >::value);
}
It looks like sizeof( loophole_t<std::string, 0> );
affects compiler global state. I mean if we remove this line static_asserts
fails. Is at allowed for C++?
UPDATE: Just realized it depends on compiler or even compiler version. Works pretty well with any GCC >=8 (probably with older versions as well). Does not compile with clang >= 10, but works fine with clang 7.0
So I'd say my real question whether it is a compiler bug or a standard behavior?