初始化大型静态类数组 [英] Initialise large static class array

查看:72
本文介绍了初始化大型静态类数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板来创建各种对象的实例.该模板具有一个静态的类声明数组,该数组应该创建一个在其创建过程中传递的类类型数组.

I have a template to create instance of various objects. The template has a static array of class declaration which is supposed to create an array of class type being passed during its creation.

在下面的示例中,myclass是类对象的静态数组,其大小为200-也可以更大.请注意,模板可以用不同的对象实例化-因此数组的类型也将相应地更改.

In below example myclass is the static array of class object with size 200 - which can be bigger also. Note that the template can be instantiated with different objects - so the type of array will also be changed accordingly.

如何在声明本身期间初始化静态数组-我知道我们在定义自身时需要初始化静态数组,如果大小更大,该怎么办-

How can initialize the static array during declaration itself - I understand that we need to initialize static array when defined itself, what if the size if more bigger -

template <class object>
A<object> myclass[200] = { .... new object 200 times...};

还是我需要它来做新的/删除模板中定义的重载运算符?在这种情况下,对象数组将如何构建和构建?破坏会发生吗?如果某些对象在模板实例化之前引用了数组,因为它们是静态的怎么办?

or I need it to do it new / delete overloaded operator as defined in template? In such a case how will the array of objects construction & destruction will occur? What if some object references the array as they are static before template instantiation?

推荐答案

如何在声明本身[?]期间初始化静态数组?

How can initialise the static array during declaration itself [?]

如果要使用默认(无参数)构造函数初始化 object ,这很容易;像

If you want initialize the objects with the default (no parameter) contructor, it's easy; something like

template <class object>
object A<object>::myclass[200] { };

以下是完整的(简化的)示例

The following is a full (simplified) example

#include <iostream>

template <typename T, std::size_t Dim>
struct foo : public T 
 { static T const myArray[Dim]; };

template <typename T, std::size_t Dim>
T const foo<T, Dim>::myArray[Dim] { };

struct bar
 { bar () { std::cout << "bar! " << std::endl; } };

int main ()
 { (void)foo<bar, 10>::myArray; } // print 10 times "bar!"

如果要使用其他构造函数对其进行初始化,则要复杂一些.

If you want initialize it with a different constructor... it's a little more complicated.

以下是C ++ 14的示例(使用 std :: index_sequence std :: make_index_sequence ;但是用C ++代替它们并不难吗?11,如果需要的话),它使用部分专业化,模板默认值,可变参数解压缩和逗号运算符

The following is a C++14 example (use std::index_sequence and std::make_index_sequence; but isn't really difficult substitute they in C++11, if you need it) that use partial specialization, template default values, variadic parameters unpacking and the comma operator

#include <utility>
#include <iostream>

template <typename T, std::size_t Dim,
          typename U = decltype(std::make_index_sequence<Dim>{})>
struct foo;

template <typename T, std::size_t Dim, std::size_t ... Is>
struct foo<T, Dim, std::index_sequence<Is...>>
 { static T const myArray[Dim]; };

template <typename T, std::size_t Dim, std::size_t ... Is>
T const foo<T, Dim, std::index_sequence<Is...>>::myArray[Dim]
    { ((void)Is, T(1))... };

struct bar
 { bar (int) { std::cout << "bar! " << std::endl; } };

int main ()
 { (void)foo<bar, 10>::myArray; } // print 10 times "bar!"

这篇关于初始化大型静态类数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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