std::allocator_traits::construct调用了错误的构造函数
std::allocator_traits::construct invokes wrong constructor
受到Haskell的启发,我尝试实现这样的std::forward_list
:
namespace std {
template <class T, class Allocator = allocator<T>>
class forward_list {
typename allocator_traits<Allocator>::template rebind_alloc<pair<forward_list<T>,T>> alloc;
typename allocator_traits<decltype(alloc)>::pointer ptr;
public:
// ...
void push_front(const T &value) {
auto newPtr = allocator_traits<decltype(alloc)>::allocate(alloc, 1);
allocator_traits<decltype(alloc)>::construct(alloc, newPtr, move(*this), value);
ptr = newPtr;
}
// ...
};
}
但是push_front
中的construct
调用复制构造函数,而不是移动构造函数。
我不明白。construct
具有作为参数的转发引用,std::pair
的构造函数也是如此。因此,来自std::move
的右值引用应该完好无损地传递。那么,为什么会发生这种情况呢?
(如果这是一个骗局,我很抱歉。Stack Exchange的搜索系统没有切断它。(
原来我错误地实现了move构造函数:
forward_list(
forward_list &&other
) : forward_list(other, other.alloc) {}
必须是:
forward_list(
forward_list &&other
) : forward_list(move(other), other.alloc) {}
相关文章:
- "error: no matching function for call to"构造函数错误
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 模板,函数使用错误的构造函数来复制我的对象
- C++ OpenCV 卡尔曼滤波器构造函数错误
- 不命名构造函数和析构函数上的类型错误
- 处理简单 cpp 类构造函数中的错误
- 类和构造函数中的函数根本不起作用,并且不返回任何错误
- 链接器错误:无法解析构造函数
- 当我尝试通过构造函数分配 char 数组时出现错误
- 没有参数的构造函数给我错误
- 使用复制构造函数的程序输出错误
- 默认构造函数上的分段错误
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- 带有 QWidget 的分段错误构造函数.Qt/C++
- C++错误构造函数参数
- 错误:构造函数未命名类型
- 错误:构造函数在此上下文中受到保护
- 没有匹配的函数错误C++构造函数
- 错误:构造函数的多个定义