在实现 C++17 的 std::any
时根据可用规范in this Wiki我偶然发现了一些对我来说似乎很荒谬的东西:
In the definition免费功能的std::any_cast
,用于从 std::any
中检索值例如,提供了 r-value 引用的重载(这是第三个):
template< class ValueType >
ValueType any_cast(any&& operand); // (3)
现在,概要下方列出了适用于重载 2 和 3 的要求(这意味着还包括 r 值重载):
2-3)
Returns*any_cast<std::remove_reference_t<ValueType>>(&operand)
定义似乎实际上不允许 move 数据!
函数调用只是重定向到基于指针的重载;关于 operand
的临时性质的信息迷路了!
是不是我不能离开任何实例?这只是wiki中的错误吗?我错了吗?
最佳答案
在撰写本文时,问题处于 WP 状态,which means :
WP - (Working Paper) - The proposed resolution has not been accepted as a Technical Corrigendum, but the full WG21/PL22.16 committee has voted to apply the Defect Report's Proposed Resolution to the working paper.
更多信息请参见此处的 lwg:http://wg21.cmeerw.net/lwg/issue2509
提议的决议确实是
For the third form, if
is_move_constructible_v<ValueType>
is true andis_lvalue_reference_v<ValueType>
is false,std::move(*any_cast<remove_reference_t<ValueType>>(&operand))
, otherwise,*any_cast<remove_reference_t<ValueType>>(&operand)
以及列出 WP 的缺陷报告列表:http://cplusplus.github.io/LWG/lwg-defects.html#2509
关于c++ - 为什么 C++17 的 std::any 不允许通过 any_cast 返回可 move 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38573454/