【问题标题】:What does it mean to "unwrap" a reference_wrapper?“解包”reference_wrapper 是什么意思?
【发布时间】:2018-01-03 02:15:09
【问题描述】:

我正在编写一个实用程序类来“unwrapstd::reference_wrapper,但我对这意味着什么有点矛盾。

boost::unwrap_reference<T>::type 被定义为U(其中Tstd::reference_wrapper<U>),但这似乎违反直觉。如果 reference_wrapper 旨在“包装”引用,那么我希望“unwrappedreference_wrapper成为引用 .

诚然,这个问题可能会引起意见或猜想,但有什么理由更喜欢U 而不是U& 作为未包装的type(除了遵循boost 模型)?是否有任何技术报告详细说明 boost::unwrap_reference 可能为 boost 的模型提供基本原理?

【问题讨论】:

    标签: c++ boost reference-wrapper


    【解决方案1】:

    我看不出它有什么不同。

    我想添加&-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.hppunwrap_reference trait 的地方之一)

    因为我们知道 class_type 不是 ref-qualified,所以将其输入到 is_callable 会更容易并且不会感到惊讶。

    【讨论】:

    • 你的推理确实有道理。我使用实用程序类的方式取决于它返回一个引用类型,但实际上我可以完全避免使用实用程序类。
    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2017-06-11
    相关资源
    最近更新 更多