C++17 数组<;双*>;到向量<;双倍>;VS2019上的系列v3样式

C++17 数组<;双*>;到向量<;双倍>;VS2019上的系列v3样式,c++17,range-v3,C++17,Range V3,我想将数组转换为向量,这样我就可以对同一类型的两个向量执行范围::视图::concat,但我很难做到这一点 我有以下代码: #include <range/v3/all.hpp> #include <array> static constexpr auto max_elements = 2000; struct PriceInfo { std::array<double*, max_elements> prices; }; auto main(

我想将
数组
转换为
向量
,这样我就可以对同一类型的两个向量执行
范围::视图::concat
,但我很难做到这一点

我有以下代码:

#include <range/v3/all.hpp>

#include <array>

static constexpr auto max_elements = 2000;

struct PriceInfo
{
    std::array<double*, max_elements> prices;
};

auto main() -> int
{
    const PriceInfo* const buf_prices = nullptr;
    const auto vec = buf_prices->prices 
                   | ranges::views::indirect 
                   | ranges::to_vector;
}
#包括
#包括


如何修复此错误?首先,您的代码包含UB,因为您从未创建PriceInfo。 第二,错误可能意味着项目配置不正确,是否设置了标准?编译器是否完全符合库的要求?如果是这样的话,它是否是合适的库分叉(例如,MSVC编译器有单独的分叉)。 第三,假设这些问题将得到解决,除非价格的所有元素都是非nullptr,否则该代码将出错

通过这种方式工作:

#include <range/v3/all.hpp>
#include <iostream>
#include <array>

static constexpr auto max_elements = 3; //  will segfault if there are null pointers

struct PriceInfo
{
    std::array<double*, max_elements> prices;
};

auto main() -> int
{
    auto a = std::array<double,3>{1.0, 2.0, 3.0};
    const PriceInfo* const buf = new PriceInfo{&a[0],  &a[1],  &a[2]};
    
    const auto vec = buf->prices 
                   | ranges::views::indirect 
                   | ranges::to_vector;
                   
    for( auto a : vec)
        std::cout << a << "\n";
}
#包括
#包括
#包括
静态constexpr auto max_elements=3;//如果存在空指针,则segfault
结构价格信息
{
标准:阵列价格;
};
auto main()->int
{
自动a=std::数组{1.0,2.0,3.0};
const PriceInfo*const buf=new PriceInfo{&a[0]、&a[1]、&a[2]};
const auto vec=buf->prices
|范围::视图::间接
|范围::到_向量;
用于(自动a:vec)

std::cout首先,您在这里取消对
nullptr
的引用(
buf_prices
).这是UB.nullptr是作为一个位置标记和测试存在的。假设有一个格式正确的指针数组。这是一个godbolt游戏区域:这是另一个具有正确数组初始化的区域:@Andrew它仍然是UB…您取消引用非易失性nullptr并写入不存在的对象。编译器允许跳过该区域或执行任何与您的同事有关的godbolt操作de在msvc2019上失败:@Andrew将“视图”更改为“视图”()后成功。MSVC编译器资源管理器VM似乎使用的是range-v3的旧版本。@casey它使用的是vcpkg安装,我使用的是相同的,因此我认为我也有相同的问题。谢谢您的帮助。