移动构造函数的强制测试?

Force test of move constructor?

我正在为指向堆上某些数据的对象实施移动 construct/assign 操作。

我正在测试移动 construct/assign 和复制 construct/assign,除了移动构造之外,所有内容都已构建并且所有工作正常。 (移动分配确实有效)。

我似乎无法让调试器运行,未到达移动构造函数代码。

我确定我缺少一些简单的东西?

移动构造函数...

//...

HeapBuffer (HeapBuffer&& other)                     // move
    : data {other.data},
      size {other.size}
{
    assert(false);              // hmm can't seem to debug to here in testing :(
    other.data = nullptr;
    other.size = 0;
}

//...

测试函数...

ado::HeapBuffer<2048> makeHeapBuffer2048()  // test move semantics
{
    ado::HeapBuffer<2048> hb2048;
    hb2048[777] = 123.0f;
    return hb2048;
}

调用测试...

beginTest ("HeapBuffer move constructor");  // hmmm can't seem to get to the move constructor,
                                            // strange because all the others work!?!?
{
    ado::HeapBuffer<2048> hb {makeHeapBuffer2048()};

    expectEquals (hb[777], 123.0f);
}

我会补充说,如果我将移动构造函数更改为...

HeapBuffer (HeapBuffer&&) = delete;

...构建在预期点失败(即被测试绊倒)。

应该会出现省略,你可以试试:

ado::HeapBuffer<2048> moved;
moved[777] = 123.0f;
ado::HeapBuffer<2048> hb{std::move(moved)};

expectEquals (hb[777], 123.0f);