为了好玩,这里有一个示例,您可以使用模板生成长指针类型。编译器将生成具有数百个间接级别的类型。
#include <iostream>
template<size_t N, typename T>
struct PointerVariable
{
T* myPtr;
PointerVariable<N - 1, T*> ptrToMyPtr;
PointerVariable(T* p) :
myPtr(p),
ptrToMyPtr(&myPtr)
{
// display pointer value
std::cout << "ptr:" << ((void*)myPtr) << std::endl;
}
};
template<typename T>
struct PointerVariable<0, T>
{
PointerVariable(T* p) {
int a = 3.3; // generate a compiler warning so we can see the names of types
}
};
int main()
{
int val = 0;
auto pp = PointerVariable<499, int>{ &val };
return 0;
}
在 MSVC++2019 上,这会产生编译器警告:
warning C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
message : while compiling class template member function 'PointerVariable<0,T *>::PointerVariable(int ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************)'
而当N=500时,会出现编译错误:
fatal error C1202: recursive type or function dependency context too complex
使用显式类型,VC++2019 允许 1491 级间接:
int
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
******************************************************************************************************************************** // 128
**************************************************************** // 64
**************** // 16
*** // 3
x = nullptr;
再添加一个指针级别发射:
fatal error C1026: parser stack overflow, program too complex
这是对编译器在变量存储和类型复杂性方面能力的练习。忽略这两个限制,例如只使用uintptr_t 而不是真正的指针类型,那么您的限制就是系统内存。