本文介绍了“结果类型必须可从输入范围的值类型构造"创建 std::vector 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的班级成员

I have a class member that looks like this

class Controller {
    protected:
    // other stuff
    std::vector<Task<event_t, stackDepth>> taskHandlers;

    //some more stuf
}

Task 类是非 defaultConstructible、非 copyConstructible、非 copyAssignable,但是是 moveConstructible 和 moveAssignable.我能读到的所有内容(特别是 std::vector 文档)都让我认为这应该可以编译,但错误列表看起来像这样(完整输出 这里) :

The Task class is non-defaultConstructible, non-copyConstructible, non-copyAssignable but is moveConstructible and moveAssignable. Everything I could read (notably, std::vector documentation) leads me to think that this should compile, but the error list looks like this (full output here) :

/usr/include/c++/9/bits/stl_uninitialized.h:127:72: error: static assertion failed: result type must be constructible from value type of input range
  127 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |                                                                        ^~~~~

使 Task 默认可构造没有帮助.错误指向Controller 类的定义.我在 c++17 模式下使用 GCC 9.3.0.我做错了什么吗?

Making Task defaultConstructible did not help. The error points to the definition of the class Controller. I am using GCC 9.3.0 in c++17 mode. Did I do anything wrong ?

推荐答案

鉴于目前的信息,我最好的猜测是您以某种方式弄乱了移动构造函数语法 - 仅使用 emplace_back 的工作示例:

My best guess, given the current information, is that you messed up the move constructor syntax somehow - working example using just emplace_back:

以下编译正常,godbolt 链接:

#include <vector>

class X
{
public:
    X(int i) : i_(i){}
    X() = delete;
    X(const X&) = delete;
    X(X&&) = default;//change this to 'delete' will give a similar compiler error
private:
    int i_;
};


int main() {
    std::vector<X> x;
    x.emplace_back(5);
}

这篇关于“结果类型必须可从输入范围的值类型构造"创建 std::vector 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:49