【发布时间】:2023-03-29 15:11:01
【问题描述】:
我在一本关于元编程的书中找到了这个工作代码 -
template<unsigned long N>
struct binary
{
static unsigned const value = binary<N/10>::value *2 + N%10;
};
template<>
struct binary<0>
{
static unsigned const value = 0;
};
int main()
{
unsigned x = binary<101010>::value;
cout << x;
}
我的问题是 - value 的内存分配在哪里?它是在数据段上分配的吗?
此外,书中说此代码会导致级联模板实例化,这些模板实例化以类似于递归的方式计算结果。这是否意味着对于每个模板实例化,都会在数据段上分配一个新的unsigned?
【问题讨论】:
标签: c++ metaprogramming template-meta-programming