将对 class 的引用转发为另一种类型

Forwarding a reference to a class as another type

我有如下代码,它通过转发引用接受 std::aligned_storage_t 参数,并且应该 reinterpret_cast 将其转换为另一种类型,然后 return 将其提供给用户。

template <typename AlignedStorageType, typename TypeToReturnAs>
decltype(auto) forward_as_another_type(AlignedStorageType&& storage) {
    return *reinterpret_cast<TypeToReturnAs*>(&storage);
}

有没有什么好的方法可以维护storage在return类型中的引用类型?例如,如果存储是一个右值引用,那么我希望 return 类型也是一个右值引用。

首先,翻转模板参数。你要推导AlignedStorageType,要显式指定另一个:

template <typename TypeToReturnAs, typename AlignedStorageType>
decltype(auto) forward_as_another_type(AlignedStorageType&& storage) {
    return *reinterpret_cast<TypeToReturnAs*>(&storage);
}

接下来,您基本上想要的是有条件地转换表达式。如果 AlignedStorageType&&X&&,您想将其转换为 TypeToReturnAs&&。如果是 X&,则为 TypeToReturnAs&。如果 X const&,如 TypeToReturnAs const&.

我们可以添加一个类型特征来匹配引用:

template <class T, class U> struct match_reference;
template <class T, class U> struct match_reference<T&, U>       { using type = U&; };
template <class T, class U> struct match_reference<T const&, U> { using type = U const&; };
template <class T, class U> struct match_reference<T&&, U>      { using type = U&&; };
template <class T, class U> using match_reference_t = typename match_reference<T,U>::type;

然后:

template <typename TypeToReturnAs, typename AlignedStorageType>
decltype(auto) forward_as_another_type(AlignedStorageType&& storage) {
    using R = match_reference_t<AlignedStorageType&&, TypeToReturnAs>;
    return static_cast<R>(*reinterpret_cast<TypeToReturnAs*>(&storage));
}

或者,如果您只是一次性使用它,您可以将该逻辑写为条件:

template <typename TypeToReturnAs, typename AlignedStorageType>
decltype(auto) forward_as_another_type(AlignedStorageType&& storage) {
    using R = std::conditional_t<
        std::is_lvalue_reference<AlignedStorageType>::value,
        TypeToReturnAs&, 
        TypeToReturnAs&&>;
    return static_cast<R>(*reinterpret_cast<TypeToReturnAs*>(&storage));
}

或:

    using R = std::conditional_t<
        std::is_lvalue_reference<AlignedStorageType>::value,
        TypeToReturnAs&, 
        TypeToReturnAs>;
    return std::forward<R>(*reinterpret_cast<TypeToReturnAs*>(&storage));