我可以将 std::数组转换为切片吗?或者我还能用什么
Can I cast a std::array to slice? Or is there anything else I can use instead?
这是未定义的行为吗?
std::array<int, 5> x = {3, 5, 1, 2, 3};
std::array<int, 3>& y = *reinterpret_cast<std::array<int, 3>*>(&x[1]);
for(int i = 0; i != 3; i++) {
std::cout << y[i] << "n";
}
也许是的,但我真的觉得应该有一种安全的方法来切片std::array
。
编辑:遵循拉狄克的建议:
template<unsigned N, unsigned start, unsigned end, typename T>
std::array<T, end - start>& array_slice(std::array<T, N>& x)
{
static_assert(start <= end, "start <= end");
static_assert(end <= N-1, "end <= N");
return *reinterpret_cast<std::array<T, end - start>*>(&x[start]);
}
编辑:好的,我决定我对std::array
不满意,并将转向其他事情,有什么想法吗?
是的,这是未定义的行为。您正在采用一种类型并将其reinterpret_cast
另一种类型。的确,使用reinterpret_cast
应该是"这里有龙"的大红旗!
至于切片数组,这不会发生。std::array
包含值;其中的一部分将包含对该数组一部分的引用。因此,这不会是一个std::array
.您可以复制数组的切片,但不能使用 std::array
。您需要使用 std::vector
,因为它允许调用构造函数,以及从一系列值进行构造。请记住:std::array
只是围绕 C 样式数组的更好包装器。
委员会正在研究一个array_ref<T>
类的模板,这正是它所说的:对类型 T
数组的某些段的引用。这可以是一个常规的C式数组,一个std::vector
,一个std::array
,或者只是一些分配了new T[]
的内存。该类已经有一些库实现,但还没有标准化的。
按照拉狄克的建议:
在函数中隐藏未定义的行为不会使其成为定义的行为。你可以试着假装它不是未定义的,但它仍然是。当你使用那reinterpret_cast
的那一刻,你心甘情愿地放弃生活在C++土地上。
新版位呢?
#include <array>
#include <iostream>
#include <iterator>
template<typename T, std::size_t N>
struct array_slice : public std::array<T,N> {
~array_slice() = delete;
};
int main() {
std::array<double,4> x_mu{0.,3.14,-1.,1.};
std:: cout << &x_mu << std::endl;
{
auto slicer = [] (std::array<double,4>& ref) {
array_slice<double,3>* p = new (&ref) array_slice<double,3>;
return p;
};
std::array<double,3>& x_ = *slicer(x_mu);
std::copy(x_.begin(),x_.end(),
std::ostream_iterator<float>(std::cout," "));
std:: cout << std::endl;
std:: cout << &x_ << std::endl;
}
std::copy(x_mu.begin(),x_mu.end(),
std::ostream_iterator<float>(std::cout," "));
}
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 计时器坏了或者其他什么的
- 我是否错过了什么,或者虚拟呼叫的性能并不像人们所说的那样糟糕
- vector::empty()出错,或者我遗漏了什么
- 继承Java集合接口(Set、Map、List等)的C++等价物是什么?或者扩展AbstractCollection
- ShellExecute不工作,或者我做错了什么
- 我的字节数组到十六进制字符串转换器有什么问题,或者为什么它在写入文件时会在某个位置后剪切符号?
- 我可以将 std::数组转换为切片吗?或者我还能用什么
- valarray在gcc和msvc(或者Linux和Windows)中有什么不同
- 我的iOS金属计算内核是否存在编译器错误,或者我遗漏了什么
- 如何派生模板函数?或者这个场景的首选方法是什么
- 有什么事情c++可以做得比D更好,或者D做不到的吗?(多重继承的例子)
- 解析是如何工作的,或者是什么使类型完整或不完整
- 构造 函数?或者别的什么
- 为什么 std::noskipws 不起作用,或者它应该做什么?