c++ - 创建 std::vector 时,“结果类型必须可以从输入范围的值类型构造”
问题描述
我有一个看起来像这样的班级成员
class Controller {
protected:
// other stuff
std::vector<Task<event_t, stackDepth>> taskHandlers;
//some more stuf
}
该类Task
是 non-defaultConstructible、non-copyConstructible、non-copyAssignable,但是是 moveConstructible 和 moveAssignable。我能读到的所有东西(特别是std::vector
文档)都让我认为这应该编译,但错误列表看起来像这样(此处的完整输出):
/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
defaultConstructible 没有帮助。错误指向类的定义Controller
。我在 c++17 模式下使用 GCC 9.3.0。我做错什么了吗?
解决方案
鉴于当前信息,我最好的猜测是您以某种方式弄乱了移动构造函数语法 - 仅使用以下示例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);
}
推荐阅读
- python - 我正在尝试编写一个 discord.py 机器人来保存和命名图像 url,然后可以调用图像
- python - Django REST - 查询集 order_by 不能按降序工作
- python - 如何在 Python 重构期间防止变量名冲突?
- scala - 对数据框中的列(PANCARD)值进行排序
- c - 尝试在函数中使用 txt 文件时获取“'->' 的无效类型参数(有 'int')”
- html - 剪辑路径在 Safari 上无法正常工作
- windows - Windows批处理命令创建备份文件夹和替换文件夹
- tensorflow - 如何在 Keras/Tf 中实现两层的张量积
- python - 使用 Python 的 Selenium 抓取 Trulia
- bash - Jenkins 中的 Shell 脚本