移动转换运算符的语义 [英] Move semantics for a conversion operator
本文介绍了移动转换运算符的语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可移动转换操作符的语法是什么?
What's the syntax for a movable conversion operator?
我有一个封装 obj
有 obj
转换运算符:
I have a wrapper that wraps around obj
, which has an obj
conversion operator:
class wrap {
public:
operator obj() { ... }
private:
obj data_;
};
如何找出 data _
推荐答案
这样的语法如下:
class wrap {
public:
operator obj() const & { ... } //Copy from me.
operator obj() && { ... } //Move from me.
private:
obj data_;
};
第一个版本将在第二个版本无法调用时调用(即: wrap
正在转换的实例不是临时的,或者没有明确使用 std :: move
)。
The first version will be called when the second version cannot be called (ie: the wrap
instance being converted is not a temporary or there is no explicit use of std::move
).
注意,Visual Studio没有实现r值引用的这个方面。
Note that Visual Studio doesn't implement this aspect of r-value references.
这篇关于移动转换运算符的语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文