我可以将 std::数组转换为切片吗?或者我还能用什么

Can I cast a std::array to slice? Or is there anything else I can use instead?

本文关键字:或者 什么 切片 std 数组 转换 我可以      更新时间:2023-10-16

这是未定义的行为吗?

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," "));
}