ELI5 函数名称后面的"&&"关键字是什么意思
ELI5 What is the meaning '&&' keyword after a function name
我遇到了这个代码,我不知道这是什么,有人可以向我解释一下吗?
template<class T> base{
protected:
T data;
public:
...
T&& unwrap() && { return std::move(data); }
operator T&&() && { return std::move(data); }
}
我知道operator T&&()
是一个演员,但我无法弄清楚粗体&&in 的含义是什么:
运算符 T&&((和&或T&& unwrap(( &&
成员函数上的&&
和&
限定符如下所示:
&
限定函数将在对象的左值实例上调用&&
限定函数将在对象的右值实例上调用
例:
#include <iostream>
class Example
{
private:
int whatever = 0;
public:
Example() = default;
// Lvalue ref qualifier
void getWhatever() & { std::cout << "This is called on lvalue instances of Examplen"; }
// Rvalue ref qualifier
void getWhatever() && { std::cout << "This is called on rvalue instances of Examplen"; }
};
int main()
{
// Create example
Example a;
// Calls the lvalue version of the function since it's called on an lvalue
a.getWhatever();
// Calls rvalue version by making temporary
Example().getWhatever();
return 0;
}
其输出为:
This is called on lvalue instances of Example
This is called on rvalue instances of Example
由于在第一个函数调用中,我们在Example
的左值实例上调用它,但在第二次调用中,我们将其设置为临时函数,我们调用函数,调用右值限定函数。
相关文章:
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 这个额外的关键字在这个 c++ 类声明中是什么意思?
- "friend"关键字在C++中是什么意思?
- 关键字使用的功能是什么?
- 统一初始化是隐式发生的,即使 int 强制转换运算符是使用 explicit 关键字声明的.原因是什么?
- ELI5 函数名称后面的"&&"关键字是什么意思
- 在 Objective-C++ 中应用于__weak指针时,通过关键字推导类型"auto"规则是什么?
- 内联函数 - 相对于内联关键字,它们到底是什么
- C++中"using"关键字背后的逻辑是什么?
- c++ 类访问关键字"signals"是什么意思?
- CppUTest中extern关键字的替代方案是什么
- 摩西源代码中的关键字可变是什么?
- C++中的"and"、"or"等关键字的目的是什么?
- 函数参数之前的类关键字是什么
- c++函数签名的附加关键字是什么
- MSVC 中"interface"关键字是什么?
- 使用' operator '关键字是什么意思?
- 在Win-32 c++应用程序中CALLBACK关键字是什么意思?
- 当重写一个方法时,虚拟关键字是什么意思
- 在c++代码中,以下关键字是什么意思?SIGSELECT,U32,U16,U8