admin管理员组文章数量:1278851
I want to apply the same template algorithm to std::vectors
which contain objects of some type T
and (different) std::vector
s which contain std::shared_ptr
s 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.
I want to apply the same template algorithm to std::vectors
which contain objects of some type T
and (different) std::vector
s which contain std::shared_ptr
s 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.
Share Improve this question edited Feb 24 at 18:38 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Feb 23 at 20:09 Damir TenishevDamir Tenishev 3,4028 silver badges21 bronze badges 2 |5 Answers
Reset to default 9You 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();
}
}
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./z/TKPefPnKr
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))();
(ifval
isS
or a reference to it)(val->*(&S::member))();
(ifval
is a pointer toS
or smart pointer toS
) (all standard smart pointers overloadoperator->*
, just like they overloadoperator->
)
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
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
本文标签:
版权声明:本文标题:c++ - How can I transparently process std::vector of T and std::vector of std::shared_ptr<T> in a template? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304037a2371262.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
c++
standard can you use ? – edrezen Commented Feb 23 at 21:09/std:c++latest
compiler option. – Damir Tenishev Commented Feb 23 at 21:15