我正在编写一个实用程序类来“展开” std::reference_wrapper
,但我对这意味着什么有点矛盾。
boost::unwrap_reference<T>::type
定义为 U
(其中 T
是 std::reference_wrapper<U>
),但这似乎违反直觉。如果 reference_wrapper
是为了“包装”一个引用,那么我会期待一个“展开”reference_wrapper
成为引用。
不可否认,这个问题可能会导致意见或猜想,但有什么理由更喜欢U
在 U&
作为展开的 type
(除了遵循 boost 模型之外)?有没有详细的技术报告boost::unwrap_reference
这可能为 boost 模型提供基本原理?
最佳答案
我看不出有什么区别。
我想添加 &
-qualifier 比使用 std::remove_reference
移除它更容易?
事实上,这可能是原因:在裸类型上进行进一步的类型组合比始终处理 const/volatile/ref 限定要容易得多。参见例如这种用法:
template<typename Class, typename TypeIn, typename TypeOut>
struct is_cnv<Class, TypeIn, TypeOut, typename enable_if<is_class<Class>, void>::type>
{
typedef typename ::boost::unwrap_reference<Class>::type class_type;
typedef void signature_type(TypeIn const&, optional<TypeOut>&);
BOOST_DECLARE_IS_CALLABLE(is_callable, operator());
BOOST_STATIC_CONSTANT(bool, value = (is_callable<class_type, signature_type>::value));
};
(在 convert/detail/is_converter.hpp
中,是少数几个实际使用 boost/core/ref.hpp 中的
)unwrap_reference
特性的地方之一
因为我们知道 class_type
没有 ref-qualified,所以更容易将它输入 is_callable
而不会感到惊讶。
关于c++ - "unwrap"a reference_wrapper 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48070321/