简介
Ref qualifiers :一种消除隐含对象的 rl-valuness 歧义的方法。作为一个简单的例子,以下面的类为例
class example { int member; public: // ... int& value() &; // ^ int&& value() &&; // ^^ int const& value() const&; // ^ };
使用此 C++11 功能(用
^
标记的语法),允许我们控制将用 调用的value()
的版本p>- 左值
- 临时工
- 常量左值
实际上 ref 限定适用于类的
*this
Defaulted / Deleted functions : 将一个特殊成员函数指定为编译器生成(默认)定义或不可访问(删除)。举个例子
struct type { type(const type&) = delete; type& operator=(const type&) = delete; };
上述结构体,实现了不可复制,语义极其清晰
问题
- 组合这些功能是否可能/有效?
- 哪些情况是明确禁止或不良风格?
- 这种组合是否有任何用例/模式? (例如,快速简便地创建基于 rl-valueness 的条件接口(interface))
最佳答案
是的,但是用处不大,因为构造函数和析构函数不能被引用限定。
您可以引用限定赋值运算符:
struct S {
S &operator=(const S &) && = default;
};
int main() {
S s;
S() = s; // OK
s = S(); // error - LHS must be an rvalue
}
但是,我有点无法想象这有什么用。
关于c++ - 显式默认/删除的函数可以在 ref 限定符上重载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038462/