Damir Tenishev
Damir Tenishev

Reputation: 3402

How can I transparently process std::vector of T and std::vector of std::shared_ptr<T> in a template?

I want to apply the same template algorithm to std::vectors which contain objects of some type T and (different) std::vectors which contain std::shared_ptrs to objects of some type T.

Can I distinguish these types in the template so that I could dereference the pointer when the object in the std::vector is a std::shared_ptr and don't do so if the type is not a std::shared_ptr ?

Here is the code:

#include <vector>
#include <memory>

struct S {
    void member() const {}
};

void fn(const auto& arr) {
    for (const auto& val : arr) {
        // This won't compile with call fn(objects) below
        const S& obj = *val;

        // I want to have something like (I understand that I mix different things, I just want to show the idea)
        const auto& obj = std::is_same<val, std::shared_ptr> ? (*val) : val;
        // Or even better
        const S& obj = std::is_same<val, std::shared_ptr> ? (*val) : val;

        obj.member();
    }
}

int main()
{
    std::vector<S> objects;
    std::vector<std::shared_ptr<S>> pointers;

    // I want make 'fn' transparent for use containers of types T and std::shared_ptr<T>
    fn(objects);
    fn(pointers);
}

It seems that I can pass "de-wrapper" functor as a second argument to the call and make access over there, but I don't want to overcomplicate the client code.

Upvotes: 8

Views: 600

Answers (5)

Ted Lyngmo
Ted Lyngmo

Reputation: 117812

You could add a type trait to check if a type is a std::shared_ptr<something>:

#include <type_traits>

template<class T>
struct is_shared_ptr : std::false_type {};

template<class T>
struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};

template<class T>
inline constexpr bool is_shared_ptr_v = is_shared_ptr<T>::value;

Example usage:

void fn(const auto& arr) {
    for (const auto& val : arr) {
        const S& obj = [&]() -> const S& {
            if constexpr (is_shared_ptr_v<std::remove_cvref_t<decltype(val)>>) {
                return *val;
            } else {
                return val;
            }
        }();

        obj.member();
    }
}

Upvotes: 9

Turtlefight
Turtlefight

Reputation: 10965

If you just want to invoke a member function, then by far the easiest option would be to just use std::invoke():

Compiler Explorer

void fn(const auto& arr) {
    for (const auto& val : arr) {
        std::invoke(&S::member, val);
    }
}

It is easy to read, easy to write and works out of the box for basically all (smart) pointer types.

In this case, std::invoke(&S::member, val) will result in one of those two invocations:

  • (val.*(&S::member))(); (if val is S or a reference to it)
  • (val->*(&S::member))(); (if val is a pointer to S or smart pointer to S) (all standard smart pointers overload operator->*, just like they overload operator->)

Upvotes: 6

Marek R
Marek R

Reputation: 38112

With use of C++20 concepts, the code is pretty nice:

template <typename T>
concept range_of_pointers = std::ranges::range<T> && requires(T r) {
    **std::begin(r);
};

template <std::ranges::range T>
decltype(auto) pointer_transparent(T&& r)
{
    return std::forward<T>(r);
}

template <range_of_pointers T>
auto pointer_transparent(T&& r)
{
    return std::views::transform(std::forward<T>(r),
        [](const auto& p) -> decltype(auto) { return *p; });
}

Live demo

And if pipe syntax it nice to have, then you can do this:

template <typename T>
concept range_of_pointers = std::ranges::range<T> && requires(T r) {
    **std::begin(r);
};

struct pointer_transparent_fn {
    template <std::ranges::range T>
    constexpr decltype(auto) operator()(T&& r) const
    {
        return std::forward<T>(r);
    }

    template <range_of_pointers T>
    constexpr auto operator()(T&& r) const
    {
        return std::views::transform(std::forward<T>(r),
            [](const auto& p) -> decltype(auto) { return *p; });
    }

    template <typename T>
    constexpr friend decltype(auto) operator|(T&& r, const pointer_transparent_fn& self)
    {
        return self(std::forward<T>(r));
    }
};

inline constexpr pointer_transparent_fn pointer_transparent;

Live demo

Upvotes: 2

edrezen
edrezen

Reputation: 1845

You can use std::views::transform to adapt the object to be iterated through a function mytransfo; this function would have a specific template specialization for handling std::shared_ptr objects.

Note that for the generic case, you could simply return the array itself, and not returning a view.

#include <vector>
#include <memory>
#include <ranges>

struct S {
    void member() const {}
};

template<typename Array>
auto mytransfo (Array const& arr) 
{
    return arr | std::views::transform([] (auto const& x) { return x; } );
};

template<typename T>
auto mytransfo (std::vector<std::shared_ptr<T>> const& arr) 
{
    return arr | std::views::transform([] (auto const& x) { return *x; } );
};

void fn(const auto& arr) 
{
    for (const auto& obj : mytransfo(arr))  {  obj.member(); }
}

int main()
{
    std::vector<S> objects;
    std::vector<std::shared_ptr<S>> pointers;
    
    fn(objects);
    fn(pointers);
}

DEMO

By doing this, your fn remains simple and you delegate the decision "object/pointer" logic to another part.


Update

According to @Jarod42's remark, one should take care about the return type of the lambda and make sure one returns a reference and not a copy. So we can use a decltype(auto) as a return type here (and not a simple auto that would decay the type and return a copy).

#include <vector>
#include <memory>
#include <ranges>
#include <iostream>

struct S 
{
    S()         { std::cout << "default\n"; }
    S(S const&) { std::cout << "copy\n";    }
    S(S     &&) { std::cout << "move\n";    }
    
    void member() const {}
};

template<typename Array>
auto mytransfo (Array const& arr) 
{
    return arr | std::views::transform([] (auto const& x) -> decltype(auto) { return x; } );
};

template<typename T>
auto mytransfo (std::vector<std::shared_ptr<T>> const& arr) 
{
    return arr | std::views::transform([] (auto const& x) -> decltype(auto) { return *x; } );
};

void fn(const auto& arr) 
{
    for (const auto& obj : mytransfo(arr))  {  obj.member(); }
}

int main()
{
    std::vector<S> objects;
    objects.push_back (S{});
    fn(objects);
 
    std::vector<std::shared_ptr<S>> pointers;
    pointers.push_back (std::make_shared<S>(S{}));
    fn(pointers);
}

DEMO

Upvotes: 3

Jeff Garrett
Jeff Garrett

Reputation: 7518

I would consider an overload set as the most straightforward option.

#include <vector>
#include <memory>

struct S {
    void member() const {}
};

const auto& deref(const auto& v) { return v; }
template <typename T> const T& deref(const std::shared_ptr<T>& ptr) { return *ptr; }

void fn(const auto& arr) {
    for (const auto& val : arr) {
        const S& obj = deref(val); 
        obj.member();
    }
}

int main()
{
    std::vector<S> objects;
    std::vector<std::shared_ptr<S>> pointers;
    
    // fn is transparent for use containers of types T and std::shared_ptr<T>
    fn(objects);
    fn(pointers);
}

https://godbolt.org/z/TKPefPnKr

Upvotes: 7

Related Questions