";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
"result type must be constructible from value type of input range" when creating a std::vector
我有一个类成员,看起来像这个
class Controller {
protected:
// other stuff
std::vector<Task<event_t, stackDepth>> taskHandlers;
//some more stuf
}
Task
类是non-defaultConstructable、non-copyConstructable和non-copyAssignable,但却是moveConstructable和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
设置为defaultConstructable没有帮助。错误指向类别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);
}
再现错误result type must be constructible from value type of input range
导螺杆
#include <vector>
#include <string>
#include <iostream>
int main() {
typedef std::vector<std::string> StrVec;
StrVec s1("a", "b"); // error
// stl_uninitialized.h:127:7: error: static_assert failed due to requirement 'is_constructible<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, const char &>::value' "result type must be constructible from value type of input range"
//StrVec s2("a", "b", "c"); // error
// error: no matching constructor for initialization of 'StrVec' (aka 'vector<basic_string<char> >')
StrVec s3({"a", "b"}); // ok
StrVec s4({"a", "b", "c"}); // ok
typedef std::vector<int> IntVec;
IntVec i1(1, 2); // silent error
for (auto i : i1) {
std::cout << "i = " << i << 'n';
}
// silent error:
// output:
// i = 2
// expected:
// i = 1
// i = 2
//IntVec i2(1, 2, 3); // error
// error: no matching constructor for initialization of 'IntVec' (aka 'vector<int>')
IntVec i3({1, 2}); // ok
IntVec i4({1, 2, 3}); // ok
}
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型