Why is the following code valid? The struct test contains a vector of test, so that the following code compiles (IDEOne):
#include <iostream>
#include <vector>
using namespace std;
struct test {
vector<test> a;
};
int main() {
// your code goes here
test t;
if (t.a.size() > 0)
return -1;
else if (t.a[0].a[0].a.size() > 0)
return 1;
return 0;
}
How does the compiler handle the struct so that is is possible to test for t.a[0].a[0].a.size()
? Is there a limit on how often I could repeat the .a[0]
?
Edit: This questions has an answer that claims this is undefined behaviour: Are C++ recursive type definitions possible, in particular can I put a vector<T> within the definition of T?
=> This is confusing
=> perhaps my question is a duplicate