How to test if a container type holds unique_ptr as its elements?

Hi,

I need a concept that specifies whether a container type's value_type is a specialization of unique_ptr, like this:

 
<vector<unique_ptr<T>>  // for some T 


1
2
3
template <typename C>
concept contains_unique_ptr
//... 


so I can use to restrict a template function to collections of unique_ptr<T>...
1
2
3
4
5
6
7
8
9
10
#include <memory>
#include <type_traits>
#include <ranges>

template < typename T > struct is_unique_ptr : std::false_type {} ;
template < typename T > struct is_unique_ptr< std::unique_ptr<T> > : std::true_type {} ;

template < typename T >
concept contains_unique_ptr = std::ranges::range<T> &&
                              bool( is_unique_ptr< std::ranges::range_value_t<T> >{} ) ;

http://coliru.stacked-crooked.com/a/dd66e43e6d9be2be
Thanks!!
> to collections of unique_ptr<T>
Hi, JLBorges.
Sorry, but, assuming that ‘collections’ might mean ‘containers’, would that concept hold true even for a std::map?
Like a std::map<char, std::unique_ptr<int>> or a std::map<std::unique_ptr<int>, char> for example?
Thank you in advance.
> would that concept hold true even for a std::map?

No. The value_type of std::map is a pair: std::pair< const key_type, mapped_type >
Topic archived. No new replies allowed.