大佬教程收集整理的这篇文章主要介绍了c – 将参数传递给“类似数组”的容器构造函数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用嵌入式平台,但有以下限制:
>没有堆
>没有Boost库
>支持C 11
我过去曾经多次处理过以下问题:
该项目最近才增加了C 11支持,到目前为止,每次我必须处理这个问题时,我一直在使用临时解决方案.既然C 11可用,我想我会尝试制定一个更通用的解决方案.
解决方案尝试
我复制了an example of std::aligned_storage来为我的数组类型提出框架.结果如下:
#include <type_Traits> template<class T,size_t N> class Array { // Provide aligned storage for N objects of type T typename std::aligned_storage<sizeof(T),alignof(T)>::type data[n]; public: // Build N objects of type T in the aligned storage using default CTORs Array() { for(auto index = 0; index < N; ++indeX) new(data + indeX) T(); } const T& operator[](size_t pos) const { return *reinterpret_cast<const T*>(data + pos); } // Other methods consistent with std::array API go here };
这是一种基本类型 – 阵列< T,N>如果T是默认可构造的,则仅编译.我对模板参数打包不是很熟悉,但是看一些例子让我得到以下结论:
template<typename ...Args> Array(Args&&... args) { for(auto index = 0; index < N; ++indeX) new(data + indeX) T(args...); }
这绝对是朝着正确方向迈出的一步.阵列< T,N>现在编译,如果传递的参数匹配T的构造函数.
我唯一剩下的问题是构造一个数组< T,N>其中数组中的不同元素具有不同的构造函数参数.我想我可以把它分成两种情况:
1 – 用户指定参数
这是我对CTOR的抨击:
template<typename U> Array(std::initializer_list<U> initializers) { // Need to handle mismatch in size between arg and array size_t index = 0; for(auto arg : initializers) { new(data + indeX) T(arg); index++; } }
除了需要处理数组和初始化列表之间的维度不匹配之外,这似乎工作正常,但有许多方法可以处理那些不重要的方法.这是一个例子:
struct Foo { explicit Foo(int i) {} }; void bar() { // foos[0] == Foo(0) // foos[1] == Foo(1) // ..etc Array<Foo,10> foos {0,1,2,3,4,5,6,7,8,9}; }
2 – 参数遵循模式
在我之前的示例中,foos使用递增列表进行初始化,类似于std :: iota.理想情况下,我想支持以下内容,其中range(@R_674_10185@返回可以初始化数组的SOMETHING.
// One of these should initialize foos with parameters returned by range(10) Array<Foo,10> foosA = range(10); Array<Foo,10> foosB {range(10)}; Array<Foo,10> foosC = {range(10)}; Array<Foo,10> foosD(range(10));
谷歌搜索告诉我std :: initializer_list不是一个“普通”容器,所以我认为我没有办法让range(@R_674_10185@根据函数参数返回一个std :: initializer_list.
同样,这里有几个选项:
>在运行时指定的参数(函数返回?)
>在编译时指定的参数(constexpr函数返回?模板?)
问题
>到目前为止,此解决方案是否存在任何问题?
>有没有人建议生成构造函数参数?除了硬编码std :: initializer_list之外,我无法在运行时或编译时想到解决方案,因此欢迎任何想法.
>适用于固定尺寸和动态尺寸的容器
>可以将不同或相同的参数传递给元素构造函数
>可以使用一个或多个(元组分段构造)参数调用构造函数,甚至可以调用不同元素的不同构造函数(具有控制反转)
const size_t SIZE = 10; std::array<int,SIZE> params; for (size_t c = 0; c < SIZE; c++) { params[c] = c; } Array<Foo,SIZE> foos(iterator_construct,¶ms[0]); //iterator_construct is a special tag to call specific constructor // also,we are able to pass a pointer as iterator,since it has both increment and dereference operators
注意:您可以通过使用自定义迭代器类完全跳过参数数组分配,该类从动态位置计算它的值.
const size_t SIZE = 10; std::array<std::tuple<int,float>,SIZE> params; // will call Foo(int,float) for (size_t c = 0; c < SIZE; c++) { params[c] = std::make_tuple(c,1.0f); } Array<Foo,piecewise_construct,¶ms[0]);
以上是大佬教程为你收集整理的c – 将参数传递给“类似数组”的容器构造函数全部内容,希望文章能够帮你解决c – 将参数传递给“类似数组”的容器构造函数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。