How can I restrict a template parameter to a certain signature

Hi,

I have a function template acting on a collection and receiving an operation whose signature is like this:

1
2
3
4
5
6
7
template<typename T>
unique_ptr<T> negate(unique_ptr<T> t)
{
	auto r = *t;
	*t = r + 4;
	return std::move(t);
}


In other words I want to restrict the operation to be a callable object which receives unique_ptr<T> and returns unique_ptr?

1
2
3
4
5
6
template<typename C, typename Oper> requires ReceiveAndReturnUnique_ptr<Oper, typename C::value_type>
void for_every(C& c, Oper op)
{
      for(auto& x : c)
             op(std::move(x));
}


I need to implement the ReceiveAndReturnUnique_ptr concept

Help will be appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <memory>
#include <ranges>
#include <vector>

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> >{} ) ;

template < std::ranges::range CNTR, typename FN >
    requires contains_unique_ptr<CNTR> &&
        requires( std::ranges::range_value_t<CNTR> v, FN f )
        { { f(v) } -> std::same_as< std::ranges::range_value_t<CNTR> > ; }
void test( CNTR&& cntr, FN fn )
{
   for( auto& ptr : cntr ) ptr = std::move( fn(ptr) ) ;
}

http://coliru.stacked-crooked.com/a/9dea013739f2f3b2
Topic archived. No new replies allowed.