简体   繁体   English

使用std :: array的代码大小

[英]code size of using std::array

I'm working on an embedded system (ARM Cortex-M0, so no Linux). 我正在研究嵌入式系统(ARM Cortex-M0,所以没有Linux)。 I've written a lot of C code for embedded platforms, but this is my first foray into C++. 我为嵌入式平台编写了很多C代码,但这是我第一次涉足C ++。

In my C code, arrays passed into functions always take up 2 parameters. 在我的C代码中,传递给函数的数组总是占用2个参数。 One for the pointer to the data and a second with the length of the array. 一个用于指向数据的指针,另一个用于指向数组的长度。 For example: 例如:

void write(uint8_t *buf, size_t bufLen, size_t writeLen);

I'm considering switching these to using std::array (introduced in C++11). 我正在考虑将这些转换为使用std::array (在C ++ 11中引入)。 It's attractive because it keeps track of its own length but doesn't do any allocation. 它很有吸引力,因为它跟踪自己的长度,但没有做任何分配。 It looks like the equivalent would be 它看起来像是等价的

template<size_t N> void write(array<uint8_t, N> *buf, size_t writeLen);

If my code ends up calling write with 10 differently-sized arrays, does the compiler end up defining 10 different functions? 如果我的代码最终用10个不同大小的数组调用write ,那么编译器最终会定义10个不同的函数吗? This seems possibly particularly bad if I define a function that takes two (or more) arrays, as the function has to be templated on 2 parameters (the size of each array): 如果我定义一个需要两个(或更多)数组的函数,这似乎特别糟糕,因为函数必须在2个参数(每个数组的大小)上进行模板化:

template<size_t N, size_t M>
void readWrite(array<uint8_t, N> *readBuf,
               array<uint8_t, M> *writeBuf, size_t writeLen);

If write() code doesn't benefit much from knowing bufLen at compile time, then I don't see any point of changing it. 如果write()代码在编译时知道bufLen没有太大的好处,那么我没有看到任何改变它的点。 I'd leave the original function prototype and add convenience wrappers on demand, like this: 我将保留原始函数原型并按需添加便利包装器,如下所示:

template<size_t N>
void write(array<uint8_t, N> &buf, size_t count) {
    write(buf.data(), buf.size(), count);
}

You can add more for different buffer-like types, eg vector and string . 您可以为不同类似缓冲区的类型添加更多内容,例如vectorstring In optimized builds all these wrappers will be trivial and inline, so no code duplication, although it's still worth checking after compilation. 在优化的构建中,所有这些包装器都是微不足道的内联,因此没有代码重复,尽管在编译之后它仍然值得检查。

The next step is to use array_view which isn't in standard yet. 下一步是使用尚未标准化的array_view But you can borrow the implementation from somewhere or craft your own easily. 但是你可以从某个地方借用实现或者轻松地创建自己的实现。 With it, you can define principal function as 有了它,您可以将主函数定义为

void write(array_view<uint8_t>, size_t);

And you don't even need any wrappers because array_view can be implicitly constructed from C arrays and various containers. 而且你甚至不需要任何包装器,因为array_view可以从C数组和各种容器中隐式构造。

In theory, yes. 从理论上讲,是的。 However the template function has to be defined in the TU where it's used. 但是,模板函数必须在使用它的TU中定义。 This means it's easily inlined. 这意味着它很容易内联。

Still, also have a look at std::vector . 不过,还要看看std::vector

As long as what you're passing are really arrays (not pointers), you can accomplish roughly the same by rewriting the function as a template, but passing a reference to a raw array rather than an std::array . 只要你传递的是真正的数组(不是指针),你可以通过将函数重写为模板来完成大致相同的操作,但是将引用传递给原始数组而不是std::array That gives us code like this: 这给了我们这样的代码:

template <size_t N>
void write(uint8_t (&buf)[N]);

So to call this, we could do something like: 所以称之为,我们可以这样做:

uint8_t buffer[256];

write(buffer);

uint8_t buffer2[512];

write(buffer2);

...and in the first call, the compiler will deduce N as being 256, and in the second as being 512. ...在第一次调用中,编译器将N推导为256,在第二次调用中推导为512。

The basic effect is the same though--at least potentially, the compiler will still generate unique code for each size of array you pass. 基本效果是相同的 - 至少可能,编译器仍将为您传递的每个数组大小生成唯一代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM