0

将 boost 的 mapped_matrix 与 std::unique_ptr 等不可复制对象一起使用是否有任何限制?我在 C++14 模式下使用 VS2017。

#include <memory>
#include "boost/numeric/ublas/matrix_sparse.hpp"

class myClass
{
    public:
    myClass() {}
    void myMethod() {}
    /*...*/
};

void f()
{
    boost::numeric::ublas::mapped_matrix<std::unique_ptr<myClass>> SparseValues;
    //SparseValues(0, 0) = std::make_unique<myClass>(); // C2280, is it impossible to assign as the move assignment seems to be missing?
    auto p = std::make_unique<myClass>();
    //SparseValues(0, 0) = std::move(p);                // C2280

    auto a = SparseValues(0, 0);                        // Succeeds?? Expected it to fail, assuming it attempts to create a copy?
    auto &b = SparseValues(0, 0);                       // C4239: warning nonstandard extension used
    //auto c = SparseValues(0, 0).operator()();         // C2280, documentation for operator() says "Returns a reference of the j-th element in the i-th row", so there should be no copy?!
    auto &d = SparseValues(0, 0).operator()();          // Suceeds??
    //auto e = SparseValues(0, 0).ref();                // C2280, ref() is not ducumented but was worth a try
    //auto &f = SparseValues(0, 0).ref();               // C2440

    // OK, it seems not possible to get a reference, so then let's directly try to access an internal object:
    // Check for nullptr:
    //if (!SparseValues(0, 0)) { /*...*/ }              // Test for null: C2678, C2088
    //if (!SparseValues(0, 0)()) { /*...*/ }            // Test for null: C2678, C2088
    //if (!SparseValues(0, 0).ref()) { /*...*/ }        // Test for null: C2240
    //SparseValues(0, 0) = std::make_unique<myClass>(); // Reassign:      C2280
    //SparseValues(0, 0)->myMethod();                   // Call method:   C2819, C2039
    //SparseValues(0, 0)()->myMethod();                 // Call method:   C2819, C2039
    //SparseValues(0, 0).ref()->myMethod();             // Call method:   C2280
    //a = std::move(p);                                 // Assignment:    C2280
    //b = std::move(p);                                 // Assignment:    C2280
    //d = std::move(p);                                 // Assignment:    C2679
}

所以这对我来说看起来很有用!我的问题是:

  • mapped_matrix 真的无法处理不可复制的对象吗?
  • 这是故意的(由于架构问题),还是我应该就此提交错误报告?
  • 但是,如果它可以与不可复制的东西一起使用,如何正确使用它?
  • mapped_matrix 的文档很漂亮,嗯,很差。有人有另一个链接提供更多信息吗?喜欢
    • functor和ref()的使用场景是什么?ref() 保证矩阵中的对象存在(如果它是默认构造的对象)是否正确?
    • 仿函数返回的类型“reference”和insert_element()返回的“true_refrence”有什么区别?听起来实施在这里流血?
    • 方法assign_temporary()的使用场景是什么?

我的解决方法当然是使用 std::shared_ptr,在那里编译没有问题......

4

0 回答 0