首页 > 解决方案 > std::reference_wrapper 解开包装器

问题描述

介绍。

在 C++ 中,我们不能创建引用容器:

std::vector<int&> vri;
In instantiation of ‘class __gnu_cxx::new_allocator<int&>’:
required from ‘class std::allocator<int&>’
required from ‘struct std::_Vector_base<int&, std::allocator<int&> >’
required from ‘class std::vector<int&>’
required from here
error: forming pointer to reference type ‘int&’
       typedef _Tp*       pointer;
                          ^~~~~~~

内部实现需要创建一个指向包含类型的指针,这导致指向引用禁止类型的指针。

幸运的是,std::reference_wrapper存在:

int x{1}, y{2}, z{3};
std::vector<std::reference_wrapper<int>> vr{x, y, z};
for (auto &v : vr)
    ++v;
std::cout << x << ' ' << y << ' ' << z << '\n';

上面的代码显示2 3 4.

问题。

我正在开发一个 C++ 过滤器实用程序,例如过滤器where接收一个容器并返回std::reference_wrapper满足条件的包含对象:

template <typename container_t> auto range(const container_t &container)
{ return std::tuple{std::begin(container), std::end(container)}; };

template <typename container_t, typename predicate_t>
auto where(const container_t &container, predicate_t predicate)
{
    auto [b, e] = range(container);
    using type = std::remove_reference_t<decltype(*b)>;
    using reference = std::reference_wrapper<type>;

    std::vector<reference> result{};

    std::copy_if(b, e, std::back_inserter(result), predicate);

    return result;
}

下面的代码显示2 3 6 7

int main()
{
    std::vector v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (auto &x : where(v, [](auto n){ return n & 0b10; }))
        std::cout << x << ' ';

    return 0;
}

但我在链接过滤器时遇到问题:

for (const auto &x :
    where(where(v, [](auto n){ return n & 0b10; }), [](auto n){ return n & 0b1; })) {
    std::cout << x << ' ';
}
no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::reference_wrapper<const std::reference_wrapper<const int> >’)
   std::cout << x << ' ';
   ~~~~~~~~~~^~~~

内部where返回std::vector<std::refernce_wrapper<int>>,所以外部将使用std::vector<std::refernce_wrapper<const std::refernce_wrapper<const int>>>

我试过什么?

为了解决这个问题,我尝试创建一个展开的模板std::reference_wrapper<T>

template <typename type_t>
struct unwrap
{
    using type = type_t;
};

template <typename type_t>
struct unwrap<std::reference_wrapper<type_t>>
{
    using type = type_t;
};

template <typename type_t>
using unwrap_t = typename unwrap<type_t>::type;

到目前为止,它看起来正在工作:

int main()
{
    using ri = std::reference_wrapper<int>;
    using rf = std::reference_wrapper<float>;
    using rri = std::reference_wrapper<ri>;
    using rrri = std::reference_wrapper<rri>;

    std::cout
        << typeid(int).name() << '\t' << typeid(unwrap_t<int>).name() << '\n'
        << typeid(float).name() << '\t' << typeid(unwrap_t<float>).name() << '\n'
        << typeid(ri).name() << '\t' << typeid(unwrap_t<ri>).name() << '\n'
        << typeid(rf).name() << '\t' << typeid(unwrap_t<rf>).name() << '\n'
        << typeid(rri).name() << '\t' << typeid(unwrap_t<rri>).name() << '\n'
        << typeid(rrri).name() << '\t' << typeid(unwrap_t<rrri>).name();

    return 0;
}

它产生了正确的名称

  i   i
  f   f
  St17reference_wrapperIiE    i
  St17reference_wrapperIfE    f
  St17reference_wrapperIS_IiEE    St17reference_wrapperIiE
  St17reference_wrapperIS_IS_IiEEE    St17reference_wrapperIS_IiEE

整数和浮点 ( int, float) 保持不变,整数和浮点包装器展开,嵌套包装器展开一层。

但它在内部不起作用where

template <typename container_t, typename predicate_t>
auto where(const container_t &container, predicate_t predicate)
{
    auto [b, e] = range(container);
    using type = unwrap_t<std::remove_reference_t<decltype(*b)>>;
    //           ^^^^^^^^ <--- Unwraps iterator's inner type
    using reference = std::reference_wrapper<type>;

    std::vector<reference> result{};

    std::copy_if(b, e, std::back_inserter(result), predicate);

    // Debug
    std::cout
        << __PRETTY_FUNCTION__ << "\n"
        << '\t' << "decltype(*b) = " << typeid(decltype(*b)).name() << '\n'
        << '\t' << "unwrap *b = " << typeid(unwrap_t<decltype(*b)>).name() << '\n'
        << '\t' << "type = " << typeid(type).name() << '\n'
        << '\t' << "reference = " << typeid(reference).name() << '\n'
        << '\t' << "unwrap type = " << typeid(unwrap_t<type>).name() << '\n';

    return result;
}

int main()
{
    std::vector v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (const auto &x :
        where(where(v, [](auto n){ return n & 0b10; }), [](auto n){ return n & 0b1; })) {
        std::cout << &x << ' ';
    }

    return 0;
}

调试日志where显示解包器在第一次调用时工作(它对类型没有任何作用),但在第二次调用时没有:

  auto where(const container_t&, predicate_t) [with container_t = std::vector<int, std::allocator<int> >; predicate_t = main()::<lambda(auto:1)>]
      decltype(*b) = i
      unwrap *b = i
      type = i
      reference = St17reference_wrapperIKiE
      unwrap type = i
  auto where(const container_t&, predicate_t) [with container_t = std::vector<std::reference_wrapper<const int>, std::allocator<std::reference_wrapper<const int> > >; predicate_t = main()::<lambda(auto:2)>]
      decltype(*b) = St17reference_wrapperIKiE
      unwrap *b = St17reference_wrapperIKiE
      type = St17reference_wrapperIKiE
      reference = St17reference_wrapperIKS_IKiEE
      unwrap type = St17reference_wrapperIKiE

在内部调用中,输入容器是std::vector<int>,所以迭代器内部类型(decltype(*b))、解包类型( ) unwrap_t<decltype(*b)>、类型( )和解type包类型(unwrap_t<type>)是int,只有referencestd::reference_wrapper

在外部调用中,输入容器是std::vector<std::reference_wrapper<const int>>并且所有类型(除了reference)都是std::reference_wrapper<const int>,就好像解包器忽略了输入类型一样。

问题。

我在拆包机上做错了什么?我认为这个问题可能与const传播有关。

代码可在Try it online!.

标签: c++templateswrapper

解决方案


我认为问题在于,它*b返回一个 const 值(因为容器是通过 const 引用传递的)。您unwrap仅适用于非常量、非易失性reference_wrapper。在这个问题上我会如下:

#include <functional>

namespace detail{
template <typename type_t, class  orig_t>
struct unwrap_impl
{
    using type = orig_t;
};

template <typename type_t, class V>
struct unwrap_impl<std::reference_wrapper<type_t>,V>
{
    using type = type_t;
};
}

template<class T>
struct unwrap {
  using type = typename detail::unwrap_impl<std::decay_t<T>, T>::type;
};

template <typename type_t>
using unwrap_t = typename unwrap<type_t>::type;

int main() {
    static_assert(std::is_same_v<const int&, unwrap_t<const int &>>);
        static_assert(std::is_same_v<const int&, unwrap_t<std::reference_wrapper<const int &>>>);
        static_assert(std::is_same_v<const int&, unwrap_t<const std::reference_wrapper<const int &>&>>);
}

这应该返回任何非 a 的原始类型reference_wrapper和 cv 限定reference_wrapper的内部类型及其引用。


说明unwrap:我将在下面调用来自 OP的原始UNWRAP版本以区分我的版本。我们想调用UNWRAP任何时候的 reference_wrapper 规范std::decay_t<T>是 a std::reference_wrapperUNWRAP现在,如果我们总是调用withstd::decay_t<T>而不是 ,这可以简单地完成T

这样做的问题是,如果T不是reference_wrapper,这将删除所有限定条件,即我们希望它UNWRAP<std::decay_t<const int>>是什么时候。intconst int

为了解决这个问题,我们定义了我们template<class type_t, class orig_t> struct unwrap_impl。我们希望始终将第一个参数的衰减类型和原始类型(衰减之前)作为第二个参数传递。然后,我们可以将一般情况orig_t作为结果类型传递(如 所做的那样using type = orig_t)。

对于规范,我们定义template<class type_t, class V> struct unwrap_impl<std::reference_wrapper<type_t>, V>. 这将适用于type_treference_wrapper,即当原始类型是reference_wrapper 的某种限定时。我们不关心第二个参数(它将是原始类型),所以我们忽略它。然后我们将reference_wrapper的内部类型作为type( using type = type_t;)。

然后我们unwrap_impl基本上通过定义来调用template<class type_t> unwrap = detail::unwrap_impl<std::decay_t<type_t>, type_t>;(这是伪代码,但我认为这使它更清楚。

一些例子:

unwrap<int> -> unwrap_impl<int, int> -> int
unwrap<const int> -> unwrap_impl<int, const int> -> const int
unwrap<std::reference_wrapper<const int>> -> unwrap_impl<std::reference_wrapper<const int>, std::reference_wrapper<const int>> -> const int
unwrap<const std::reference_wrapper<const int>> -> unwrap_impl<const std::reference_wrapper<const int>, const std::reference_wrapper<const int>> -> const int

(再次更多的伪代码,但我希望它清楚)

编辑:修复了一些错误。

Edit2:似乎工作:链接


推荐阅读