微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 移动构造函数和初始化列表

我想为某个类型实现移动构造函数(没有复制构造函数),这个类型需要是boost :: unordered_map中的值类型.我们称之为复合型.

Composite具有以下签名:

struct Base
{
  Base(..stuff,no default ctor) : initialization list {}
  Base(Base&& other) : initialization list {} 
}

struct Composite
{
  Base member;
  Composite(..stuff,no default ctor) : member(...) {}
  Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure this invokes the move ctor of Base
}

我想写这个,所以boost :: unordered_map< Key,Composite>不需要复制构造函数,只使用移动构造函数.如果可能的话,我不想在Composite的移动构造函数的初始化列表中使用Base的复制构造函数.

这可能吗?

解决方法

说成员(std :: move(other.member)).

作为一个黄金法则,无论何时你通过右值引用来获取东西,你需要在std :: move中使用它,并且每当你通过通用引用(即使用&&推导出的模板类型)时,你需要在里面使用它的std ::前进.

原文地址:https://www.jb51.cc/c/110671.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐