- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在具有 const 成员的类的实例上使用“emplace_back”时遇到问题。
请参阅下面的示例代码 list 。
#include <vector>
using std::vector;
class Data {
public:
Data(int a, int b, int c) : a(a), b(b), c(c) {}
// Removing any/all of these constructors changes nothing!
Data& operator=(const Data& s) = default;
Data(const Data& s) = default;
Data(Data&& s) = default;
~Data() = default;
const int a;
const int b;
const int c;
};
int main() {
vector<Data> v;
Data e(1,2,3);
// Problem line
v.emplace_back(4,5,6);
}
编译错误:
% clang++ a.cc -std=c++11
In file included from a.cc:1:
In file included from /usr/include/c++/4.6/vector:69:
/usr/include/c++/4.6/bits/vector.tcc:319:16: error: overload resolution selected
deleted operator '='
*__position = _Tp(std::forward<_Args>(__args)...);
~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/vector.tcc:102:4: note: in instantiation of function
template specialization 'std::vector<Data, std::allocator<Data>
>::_M_insert_aux<int, int, int>' requested here
_M_insert_aux(end(), std::forward<_Args>(__args)...);
^
a.cc:25:5: note: in instantiation of function template specialization
'std::vector<Data, std::allocator<Data> >::emplace_back<int, int, int>'
requested here
v.emplace_back(4,5,6);
^
a.cc:9:11: note: candidate function has been explicitly deleted
Data& operator=(const Data& s) = default;
^
In file included from a.cc:1:
In file included from /usr/include/c++/4.6/vector:60:
/usr/include/c++/4.6/bits/stl_algobase.h:546:18: error: overload resolution
selected deleted operator '='
*--__result = std::move(*--__last);
~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/stl_algobase.h:578:14: note: in instantiation of
function template specialization 'std::__copy_move_backward<true, false,
std::random_access_iterator_tag>::__copy_move_b<Data *, Data *>' requested
here
return std::__copy_move_backward<_IsMove, __simple,
^
/usr/include/c++/4.6/bits/stl_algobase.h:588:19: note: in instantiation of
function template specialization 'std::__copy_move_backward_a<true, Data
*, Data *>' requested here
return _BI2(std::__copy_move_backward_a<_IsMove>
^
/usr/include/c++/4.6/bits/stl_algobase.h:659:14: note: in instantiation of
function template specialization 'std::__copy_move_backward_a2<true, Data
*, Data *>' requested here
return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
^
/usr/include/c++/4.6/bits/vector.tcc:313:4: note: in instantiation of function
template specialization 'std::move_backward<Data *, Data *>' requested
here
_GLIBCXX_MOVE_BACKWARD3(__position.base(),
^
/usr/include/c++/4.6/bits/stl_algobase.h:664:48: note: expanded from:
#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
^
/usr/include/c++/4.6/bits/vector.tcc:102:4: note: in instantiation of function
template specialization 'std::vector<Data, std::allocator<Data>
>::_M_insert_aux<int, int, int>' requested here
_M_insert_aux(end(), std::forward<_Args>(__args)...);
^
a.cc:25:5: note: in instantiation of function template specialization
'std::vector<Data, std::allocator<Data> >::emplace_back<int, int, int>'
requested here
v.emplace_back(4,5,6);
^
a.cc:9:11: note: candidate function has been explicitly deleted
Data& operator=(const Data& s) = default;
^
2 errors generated.
看到这个错误信息:
a.cc:9:11: note: candidate function has been explicitly deleted
Data& operator=(const Data& s) = default;
我尝试定义一个赋值运算符(以及移动和复制构造函数,以及一个析构函数)以确保隐式生成的运算符不会以某种方式变成私有(private)的。那并没有改变任何东西。我不明白为什么在这种情况下赋值运算符被“显式删除”。
请注意,将类成员更改为非常量不会导致编译错误。
最佳答案
你的 clang 版本是多少,你的代码可以用 clang和 gcc .
当然,不可能默认 operator=
因为它无法更新 const 成员,但是 vector 只需要 MoveConstructible
/CopyConstructible
并且不要使用 operator=
并且您的代码是合法的。
关于c++ - 无法 emplace_back 具有 const 成员的类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22719534/
我正在尝试创建执行搜索的线程 vector 。这是我的 SearchThread 类中的重要内容。 class SearchThread { explicit SearchThread
给定以下代码 测试.h: #ifndef __graph_aufbau_header__ #define __graph_aufbau_header__ #include #include #in
关于 someFunctionOne 和 someFunctionTwo。 当每个都放置到 map 中时,最终结果是否仍然相同? 指针是否升级为智能指针? struct MyStruct {
我对 emplace_back 的行为有点困惑。一起来看看吧。 struct innerObject { innerObject(int ); }; class outterObject {
我尝试将类 cBar 的两个实例放置到具有 emplace_back 函数的 vector 中。 根据reference调用 emplace_back 仅保留 vector 中的位置,然后“就地”创建
我想知道是否可以使用 emplace_back 将项目存储到 vector 中,emplace_back 是一种派生自 vector 所期望的类的类型。 例如: struct fruit {
这是我日常工作中的一段代码。我只想问你这两种情况有什么区别,尤其是在性能方面。 std::vector > aVec; // case 1 aVec.emplace_back("hello", "bo
我向你展示我的问题 我有 2 个列表,将它们命名为 A 和 B。 list > A = {{1},{2},{3}}; list > B = {{4},{5},{6}}; 我想要的是 A = {{1,4
#include #include using namespace std; class test{ public: test(){
似乎添加默认构造函数会阻止调用 emplace_back 并产生错误消息:“静态断言失败:类型不可分配”(gcc 5.3 with -std=c++14)。这是一个说明问题的简单代码: class A
我编写了一个简单的程序来尝试在标准库容器中就地创建对象。这是我写的: #include #include class AB { public: explicit AB(int n);
我想知道我是否正确理解emplace_back #include using namespace std; struct Hero { Hero(const string&) {}
这个问题在这里已经有了答案: Emplace an aggregate in std::vector (3 个答案) 关闭 4 年前。 为什么我可以直接调用构造函数,但是当我在 emplace_ba
我正在尝试为我的用户定义结构使用 emplace_back: #include #include #include struct IDNumber { IDNumber(std::vec
考虑下面的例子: #include #include class S { public: S() { puts("S()"); } S(int) { puts("S(int)"); }
我正在创建一个指向新对象的新指针,并立即将 push_front 放入双端队列。我想改为使用 emplace_front,但遇到编译器错误。 我的对象构造函数需要 1 个字符串参数。 std::deq
所以我一直在制作一个对象池类,它是这样使用的: class MagicTrick { public: MagicTrick(int magic) : _magic(magic) {}
#include #include struct T{ T(){ std::cout vec; vec.push_back(T()); vec.push_
我在实现我自己的模板类时遇到了困难,我想在其中添加我自己的 emplace_back 函数实现。由于我仍在学习模板设计,非常感谢您在这里提供意见。 template class MydataStru
这个问题在这里已经有了答案: emplace_back() does not behave as expected (2 个答案) 关闭 8 年前。 我很好奇这段代码的输出结果。 #include
我是一名优秀的程序员,十分优秀!