Skip to main content
se eliminó 1 carácter en el cuerpo
Origen Enlace
typename container_t::value_type>value_type
typename container_t::value_type>
typename container_t::value_type
Origen Enlace

Usé el compilador de visual studio para ver que otra información sobre el tipado informaba. El resultado que mostró en consola fue:

        decltype(*b) = int
        underlying_type = int
        type = int
        reference = class std::reference_wrapper<int const >
        decltype(*b) = class std::reference_wrapper<int const >
        underlying_type = class std::reference_wrapper<int const >
        type = class std::reference_wrapper<int const >
        reference = class std::reference_wrapper<class std::reference_wrapper<int const > const >

Evidentemente hay algún problema con la resolución del unwrap_t tanto en visual studio como en gcc. La cuestión es que si reemplazamos

decltype(*b) 

por

typename container_t::value_type>

el resultado en consola es:

        decltype(*b) = int
        underlying_type = int
        type = int
        reference = class std::reference_wrapper<int const >
        decltype(*b) = class std::reference_wrapper<int const >
        underlying_type = class std::reference_wrapper<int const >
        type = int
        reference = class std::reference_wrapper<int const >

Pero como el const se propaga el compilador da el siguiente error:

error: ambiguous overload for ‘operator=’ (operand types are ‘std::back_insert_iterator<std::vector<std::reference_wrapper<int>, std::allocator<std::reference_wrapper<int> > > >’ and ‘const int’)

Esto se soluciona si hacemos const al tipo contenido por el vector que estamos creando.

#include <iostream>
#include <vector>
#include <tuple>
#include <functional>
#include <algorithm>
#include <type_traits>

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

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;

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

    std::vector<reference> result{};

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

    std::cout
        << __PRETTY_FUNCTION__ << "\n"
        << '\t' << "decltype(*b) = " << typeid(decltype(*b)).name() << '\n'
        << '\t' << "underlying_type = " << typeid(underlying_type).name() << '\n'
        << '\t' << "type = " << typeid(type).name() << '\n'
        << '\t' << "reference = " << typeid(reference).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 & 0b10; }))
        std::cout << x << '\n';

    return 0;
}

Resultado en gcc:

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
    underlying_type = i
    type = i
    reference = St17reference_wrapperIKiE
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
    underlying_type = St17reference_wrapperIKiE
    type = i
    reference = St17reference_wrapperIKiE
2
3
6
7

Resultado en visual studio:

        decltype(*b) = int
        underlying_type = int
        type = int
        reference = class std::reference_wrapper<int const >
        decltype(*b) = class std::reference_wrapper<int const >
        underlying_type = class std::reference_wrapper<int const >
        type = int
        reference = class std::reference_wrapper<int const >
2
3
6
7