已存在的std :: array中的std :: initializer列表,而无需枚举每个元素 [英] std::initializer list from already existing std::array without enumerating each element

查看:71
本文介绍了已存在的std :: array中的std :: initializer列表,而无需枚举每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有一个带有以下构造函数的类:

Lets assume that I have a class with the following constructor:

class Foo {
    Foo(std::initializer_list<uin8_t> args)
    {
     ....
    }
}

,我有以下数组:std::array<uint8_t, 6> bar.

现在我想用bar数组在foo之外创建一个对象.还有其他方法吗?

Now I would like to create an object off foo with the bar array. Is there an other way as doing it in the follwing way:

Foo f (bar[0], bar[1], bar[2], bar[3], bar[4], bar[5]);

这种方式似乎有点复杂,感觉好像不是应该的方式.

This way seems a bit complicated and it feels like this is not the way it should be.

那么,我可以在不枚举每个数组元素的情况下从现有数组创建std::initializer list吗?

So, can I create a std::initializer list from an already existing array, without enumerating each array element?

推荐答案

不,您不能这样做.此API仅接受initializer_list,并且不具有接受一对迭代器或一个指针加上一个大小的功能,这是有缺陷的.实际上,您几乎不会在野外看到像这样的API.

No, you can't do it. This API which only accepts an initializer_list and has no facility to accept, say, a pair of iterators, or a pointer plus a size, is deficient. You'll virtually never see an API like this in the wild.

这篇关于已存在的std :: array中的std :: initializer列表,而无需枚举每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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