Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Boost多数组复制构造函数_C++_Boost_Copy Constructor_Boost Multi Array - Fatal编程技术网

C++ Boost多数组复制构造函数

C++ Boost多数组复制构造函数,c++,boost,copy-constructor,boost-multi-array,C++,Boost,Copy Constructor,Boost Multi Array,我在理解boost::multi\ux:array的复制构造函数实现时遇到了一个问题 当我尝试以下方法时 std::vector<double> a; std::vector<double> b; a.resize(12); b.resize(10); a=b; std::向量a; std::载体b; a、 调整大小(12); b、 调整大小(10); a=b; 一切顺利 但是当我尝试的时候 boost::multi_array<double,1> a;

我在理解boost::multi\ux:array的复制构造函数实现时遇到了一个问题

当我尝试以下方法时

std::vector<double> a;
std::vector<double> b;
a.resize(12);
b.resize(10);
a=b;
std::向量a;
std::载体b;
a、 调整大小(12);
b、 调整大小(10);
a=b;
一切顺利

但是当我尝试的时候

boost::multi_array<double,1> a;
boost::multi_array<double,1> b;
a.resize(boost::extents[12]);
b.resize(boost::extents[10]);
a=b;
boost::多线程阵列a;
boost::多_阵列b;
a、 调整大小(boost::extensts[12]);
b、 调整大小(boost::extensts[10]);
a=b;
我撞车了

我期望有同样的行为,但在文档中也找不到任何有用的东西

有人有主意吗

问候


awallrab

它看起来像是
boost::multi_array
的工作原理与
std::valarray
关于分配,即两个数组的大小必须匹配

根据:

只要形状匹配,每个阵列类型
多阵列
多阵列
子阵列
阵列视图
都可以从其他阵列类型中进行分配


boost::multi_数组赋值运算符的语义糟糕透顶,浪费了无数开发人员的时间来搜索像这样的答案,更糟糕的是,它引入了世界各地的项目中有多少微妙的bug没有被发现,因为我最能形容为可笑的傲慢的原因

您可以通过以下方式修复multi_array.hpp中的分配运算符:

  // replacement for arrogant boost::multi_array assignment semantics
  template <typename ConstMultiArray>
  multi_array& operator=(const ConstMultiArray& other) {
    // deallocate
    deallocate_space();
    base_ = 0;
    allocated_elements_ = 0;
    // copy members of const_multi_array_ref
    storage_ = other.storage_;
    extent_list_ = other.extent_list_;
    stride_list_ = other.stride_list_;
    index_base_list_ = other.index_base_list_;
    origin_offset_ = other.origin_offset_;
    directional_offset_ = other.directional_offset_;
    num_elements_ = other.num_elements_;
    // allocate
    allocator_ = other.allocator_;
    allocate_space();
    // iterator-based copy
    std::copy(other.begin(),other.end(),this->begin());
    return *this;
  }

  multi_array& operator=(const multi_array& other) {
    if (&other != this) {
      // deallocate
      deallocate_space();
      base_ = 0;
      allocated_elements_ = 0;
      // copy members of const_multi_array_ref
      storage_ = other.storage_;
      extent_list_ = other.extent_list_;
      stride_list_ = other.stride_list_;
      index_base_list_ = other.index_base_list_;
      origin_offset_ = other.origin_offset_;
      directional_offset_ = other.directional_offset_;
      num_elements_ = other.num_elements_;
      // allocate
      allocator_ = other.allocator_;
      allocate_space();
      // copy
      boost::detail::multi_array::copy_n(other.base_,other.num_elements(),base_);
    }
    return *this;
  }
//替换boost::multi_数组分配语义
模板
multi_数组和运算符=(const ConstMultiArray和其他){
//解除分配
释放_空间();
基数=0;
分配的元素=0;
//复制const\u multi\u数组\u ref的成员
存储=其他。存储;
范围列表=其他。范围列表;
步幅列表=其他。步幅列表;
索引\基本\列表\其他。索引\基本\列表\其他;
原点偏移量=其他。原点偏移量;
方向偏移量=其他。方向偏移量;
num_elements_uu=other.num_elements;
//分配
分配器=其他。分配器;
分配_空间();
//基于迭代器的复制
复制(other.begin(),other.end(),this->begin());
归还*这个;
}
多维数组和运算符=(常量多维数组和其他){
如果(&其他!=此){
//解除分配
释放_空间();
基数=0;
分配的元素=0;
//复制const\u multi\u数组\u ref的成员
存储=其他。存储;
范围列表=其他。范围列表;
步幅列表=其他。步幅列表;
索引\基本\列表\其他。索引\基本\列表\其他;
原点偏移量=其他。原点偏移量;
方向偏移量=其他。方向偏移量;
num_elements_uu=other.num_elements;
//分配
分配器=其他。分配器;
分配_空间();
//抄袭
boost::detail::multi_数组::copy_n(other.base_,other.num_elements(),base_);
}
归还*这个;
}
与本例中boost作者的无关信念相反,这种修复不会破坏任何东西。它不会破坏任何东西的原因是,如果大小不匹配,原始库的行为是使用断言直接崩溃,而新代码将首先调整multi_数组的大小,然后不崩溃。因此,除非您有需要应用程序崩溃的测试用例,否则此更改不可能破坏任何测试用例

节省了无数个小时的工作效率,并防止了无数未被发现的错误绝对胜过了作者可能拥有的任何语义意识形态。那里那是我的咆哮。不客气