用 std/tr1/boost::array 替换内置数组总是安全的吗?
boost::array
(或 tr1
或 std
版本)在内置数组上提供了一些不错的附加功能。
到目前为止,我们的代码库仅包含内置数组,例如(编造的,但风格匹配):
WORD m_lastReadFlags[FLAGS_MAX];
...
WORD flagBuffer[FLAGS_MAX];
if (getFlags(flagBuffer)) {
memcpy(m_lastReadFlags, flagBuffer, sizeof(m_lastReadFlags));
...
我想人们会明白的。
现在,我的问题是,对于代码中那些放置 boost::array
有意义的地方(因为进行了其他更改),array
是否是 100%为内置数组保留插入的语义? (可能的编译器错误是可以的——只有无声的行为变化才是困扰我的问题。)
也就是说,可以上面的代码被重写(例如)以使用:
boost::array<WORD, FLAGS_MAX> m_lastReadFlags;
和memcpy(可能适合使用c_array()
或data()
)和其他类似数组的访问会保持不变吗? 是的,当然我也可以用数组替换本地缓冲区并删除 memcpy
或使用 std::copy
或类似的东西,但重点是这个问题是关于内置数组和数组类的兼容性。
更新:特别困扰我的一件事是内置数组所在的位置(例如在 memcpy
情况下)数组用作指针。所有发生的事情都会被捕获吗 编译器/处理正确吗?
任务呢?
T arr1[N]; // or array<T, N>
T arr2[N]; // or array<T, N>
T* p1;
...
// Note, not all combinations will compile:
arr1 = arr2;
p1 = arr1;
arr2 = p1;
...
boost::array
(or the tr1
or std
version) offer some nice additional features over a built-in array.
Up to now, our codebase only contains built-in arrays, for example (made up, but the style matches):
WORD m_lastReadFlags[FLAGS_MAX];
...
WORD flagBuffer[FLAGS_MAX];
if (getFlags(flagBuffer)) {
memcpy(m_lastReadFlags, flagBuffer, sizeof(m_lastReadFlags));
...
I think one'll get the idea.
Now, my question is, for those places in the code, where dropping in boost::array
would make sense (because of other changes made), is array
a 100% semantics preserving drop-in for the built in array? (Possible compiler errors are OK -- only silent behavioral changes are what's bothering me.)
That is, could above code be re-written (for example) to use:
boost::array<WORD, FLAGS_MAX> m_lastReadFlags;
and the memcpy (possibly adapted to use c_array()
or data()
) and other array-like access would remain the same? Yes, of course I could also replace the local buffer by an array and remove the memcpy
or use std::copy
or something like that, but the point of this question is about the compatibility of built-in arrays and the array class.
Update: One thing that's bothering me specifically is the places (like in the memcpy
case) where the built-in arrays are used as pointers. Will all occurences be caught by the
compiler / handled correctly?
What about assignment?
T arr1[N]; // or array<T, N>
T arr2[N]; // or array<T, N>
T* p1;
...
// Note, not all combinations will compile:
arr1 = arr2;
p1 = arr1;
arr2 = p1;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这应该没问题,因为
array
类正是自动数组的包装器。它具有与方括号相同的访问语法,如果您需要获取指针,您知道如何去做。您甚至可以在任何地方使用 std::copy 并使用迭代器;无论如何,这很可能会由 memcpy 实现。array
类是聚合类型(没有重要的构造函数/析构函数/赋值),因此您可以使用传统的聚合(大括号)初始化程序来初始化它,就像普通数组。Yes, that should be fine, since the
array
class is precisely a wrapper for an automatic array. It has the same access syntax with square brackets, and if you need to get at the pointer, you know how to do it. You can even usestd::copy
everywhere and use iterators; chances are that that will be implemented bymemcpy
anyway.The
array
class is of aggregate type (no non-trivial constructors/destructor/assignment), so you can initialize it with the traditional aggregate (brace) initializer, just like a plain array.