C ++ 11稀疏矩阵实现的兼容性

时间:2014-06-15 10:38:10

标签: c++ c++11 boost ublas

我尝试使用compressed_matrix作为构建器创建coordinate_matrix

#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>

using namespace boost::numeric::ublas;

int main(int argc, char** argv) {
  coordinate_matrix<int> m1(100, 100, 100);

  for (int i = 0; i < 100; i++)
    m1.insert_element(i,i,i);

  compressed_matrix<int> m2(m1, 100);
}

这似乎使用boost 1.54和clang工作正常,但是当我使用std = c ++ 11编译它时,会抛出错误:

 choeger@daishi /tmp % clang++ test.cpp --std=c++11
In file included from test.cpp:1:
In file included from /usr/include/boost/numeric/ublas/io.hpp:18:
In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/sstream:38:
In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/istream:38:
In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/ios:40:
In file included from /usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/bits/char_traits.h:39:
/usr/bin/../lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/bits/stl_algobase.h:147:7: error: no matching function for call to 'swap'
      swap(*__a, *__b);

boost 1.54和C ++ 11是否存在已知的不兼容性?或者我是否犯了一些C ++ 11错误? 1.55 changelog没有提及ublas和矩阵,所以我猜它仍然存在。

gcc 4.8.2和clang 3.4

都会发生这种情况

2 个答案:

答案 0 :(得分:3)

这绝对是Boost中的一个错误。问题是当定义BOOST_UBLAS_STRICT_MATRIX_SPARSE时,coordinate_matrix及其迭代器类型使用代理类作为reference类型:

#ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
        typedef T &reference;
#else
        typedef sparse_matrix_element<self_type> reference;
#endif

违反了C ++标准的要求,即前向迭代器的reference类型是真正的引用。 C ++ 11 [forward.iterators] / 1状态:“如果X是一个可变迭代器X,那么类或指针类型reference满足前向迭代器的要求}是对T的引用;如果Xconst迭代器,则reference是对const T的引用,...“。

尽管有这样的事实,coordinate_matrix迭代器声称是双向迭代器,导致未定义的行为。

答案 1 :(得分:1)

回答我自己的问题:

感谢@ cv_and_he的评论,我认为C++ reference的以下部分与此相关(因为coordinate_matrix调用std::sort):

  

随机访问迭代器到的初始和最终位置   要排序的序列。使用的范围是[first,last),其中包含   第一个和最后一个之间的所有元素,包括指向的元素   首先但不是最后指向的元素。 RandomAccessIterator的   应将指向正确定义交换的类型,哪个是   可移动构造和移动分配。

显然,交换方法没有正确定义(无论这意味着什么)。

报告错误here