是否可以使用模板元编程创建和初始化值的数组? [英] Is it possible to create and initialize an array of values using template metaprogramming?

查看:204
本文介绍了是否可以使用模板元编程创建和初始化值的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时使用模板元编程创建一个计算值数组(为简单起见,我想让每个值都是它的索引的平方)。这可能吗?如何在数组中的每个位置被初始化?

I want to be able to create an array of calculated values (let's say for simplicity's sake that I want each value to be the square of it's index) at compile time using template metaprogramming. Is this possible? How does each location in the array get initialized?

(是的,有更简单的方法来做这个没有求助于模板元编程,只是想知道是否可以这样做一个数组。)

(Yes, there are easier ways to do this without resorting to template metaprogramming, just wondering if it's possible to do this with an array.)

推荐答案

虽然你不能像这样初始化一个数组,创建递归 struct

Although you can't initialise an array in-place like that, you can do almost the same thing by creating a recursive struct:

template <int I>
struct squared {
    squared<I - 1> rest;
    int x;
    squared() : x((I - 1) * (I - 1)) {}
};

template <>
struct squared<1> {
    int x;
    squared() : x(0) {}
};

然后在代码中可以声明:

Then later in your code you can declare:

squared<5> s;

,编译器会创建一个 struct 包含5 int s:0,1,4,16,25。

and the compiler will indeed create a struct containing 5 ints: 0, 1, 4, 16, 25.

p>

A couple of notes:


  1. 我对C ++标准的解释是它不能保证这个 struct 将按照与数组相同的方式进行布局。虽然它是POD类型,并且POD类型被保证在存储器(1.8 / 5)中与在偏移量0(9.2 / 17)的第一成员以及在较高地址(9.2 / 12)的稍后成员连续地和数组也被连续地布局(8.3.4 / 1),该标准没有说数组与 struct 布局兼容 c> s。然而,任何合理的编译器将这些对象放出相同的。

  2. C ++要求即使是一个空的 struct 至少1个字节长。如果没有,我们可以使用一个稍清洁的配方,其中递归的基础案例 I == 0 ,我们没有从<$ c中减去1 $ c> I

  1. My interpretation of the C++ standard is that it stops short of guaranteeing that this struct will be laid out identically to an array. While it is a POD type, and POD types are guaranteed to be laid out "contiguously" in memory (1.8/5) with the first member at offset 0 (9.2/17) and later members at higher addresses (9.2/12), and arrays are also laid out "contiguously" (8.3.4/1), the standard doesn't say that arrays are layout-compatible with such structs. However, any sane compiler will lay these objects out identically.
  2. C++ requires that even an empty struct be at least 1 byte long. If it did not, we could go with a slightly cleaner formulation in which the base case of the recursion was I == 0 and we didn't subtract 1 from I for the calculations.

如果我们可以把这个 struct 在一个联合内有一个适当大小的数组,以便于访问成员。不幸的是,如果该对象有一个非平凡的构造函数,C ++禁止你在 union 中包含一个对象。因此,获得 i th元素的最简单的方法是使用一个好的老式演员:

It would be nice if we could place this struct inside a union with an array of the appropriate size, to make it easy to access the members. Unfortunately, C++ bans you from including an object in a union if that object has a non-trivial constructor. So the easiest way to get at the ith element is with a good old-fashioned cast:

squared<5> s;
cout << "3 squared is " << reinterpret_cast<int*>(&s)[3] << endl;

如果需要,可以写一个重载的 operator [] / code>函数模板,使这个更漂亮。

If you wanted, you could write an overloaded operator[]() function template to make this prettier.

这篇关于是否可以使用模板元编程创建和初始化值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆