4
为什么下面的代码有效?该结构包含测试测试向量,所以下面的代码compiles (IDEOne):C++递归类型定义
#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;
}
编译器如何处理结构,这样是可以测试t.a[0].a[0].a.size()
?有多少次我可以重复.a[0]
?
编辑:此问题已声称这是不确定的行为答案:Are C++ recursive type definitions possible, in particular can I put a vector<T> within the definition of T?
=>这是令人困惑
=>也许我的问题是重复的
对'的std :: VECTOR'默认构造函数不要求模板参数是完全限定的名称。 – paddy
为了将事物推得更远,'struct WTF:std :: vector {};'我用它来构建[Peano的自然数](https://en.wikipedia.org/wiki/Peano_axioms):D –
YSC
(提示: 'operator ++()'只是'{push_back(* this); return * this;}') – YSC