std::result_of for lvalue/rvalue arguments
std::result_of for lvalue/rvalue arguments
我在玩耍时注意到std::result_of
的这种行为:
struct Foo {
int operator()(const int&) const { ... }
char operator()(int&&) const { ... }
};
result_of_t<Foo(const int&)> a; // int
result_of_t<Foo(int&&)> b; // char
result_of_t<Foo(int)> c; // char -- why?
为什么std::result_of
更喜欢第三种情况的RVALUE参考的功能?
std::result_of
当给定的非参数参数假定它们是rvalues时。
实际上,在几乎所有情况下,std::result_of_t<A(B)>
与std::result_of_t<A(B&&)>
相同。
如果您想了解原因,您可以在这里看到一些可能的实现。基本上,result_of_t<A(B)>
执行decltype( std::declval<A>()(std::declval<B>()) )
(忽略成员函数指针案例),并且B&&
RVALUE参考和临时B
将调用A
上任何operator()
的过载。
有三个主要值类别(lvalue,prvalue和xvalue),三个参考资格符(无,&
和&&
)。
显然,&
应指定LVALUE类别,而&&
应指定XVALUE是有道理的。因此,省略的参考预选程序应指定Prvalue。
请注意,这与使用相应返回类型的功能相同:
int f(); // returns prvalue int
int& f(); // returns lvalue reference to int
int&& f(); // returns xvalue reference to int
在您的情况下:
const int& f();
int&& g();
int h();
decltype(Foo{}(f())) a; // int
decltype(Foo{}(g())) b; // char
decltype(Foo{}(h())) c; // char
因此,您可以看到result_of
只是显示decltype
告诉您的内容。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 具有变量Number of Arguments的std::函数的矢量
- 函数返回的 rvalue 引用(表达式)是 xvalue - 但没有标识?
- 如何修复"too few arguments to function"?
- 开放CV垫"Sizes of input arguments do not match"的问题
- 有没有办法在初始化字符串时避免来自 clang-tidy(fuchsia-default-arguments)的警告?
- glGenVertexArrays and glGenBuffers arguments
- 'type_alias<char[N]>{}' VS 'char[N]{}' in function's arguments
- 如果没有带有函数签名的 rvalue 参数,是否会执行 C++ 11 中的移动语义?
- 警告:返回对临时 - 奇怪情况的引用(对 Rvalue 的澄清)
- C++标准是否保证失败的插入到关联容器中不会修改 rvalue-reference 参数?
- 未执行lvalue到rvalue转换
- 什么是可修改的rvalue
- 为什么捕获std :: Isringstream的RVALUE的OP会失败
- 是任务的正确操作数总是转换为rvalue
- RVALUE参考与const lVALUE参考作为参数之间的混淆
- Command Line Arguments wxCmdLineArgsArray in wxWidgets
- 如何使对象通过RVALUE参考通过而没有复制
- 与构造函数中rvalue结合的非const lvalue有关的错误
- std::result_of for lvalue/rvalue arguments