1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <functional>
#include <vector>
#include <list>
#include <deque>
struct object
{
void draw( double zoom ) const
{ std::cout << this << " object::draw(" << zoom << ")\n" ; }
void animate( int interval )
{ std::cout << this << " object::animate(" << interval << ")\n" ; }
void mem_fun_foo( int a, double b ) const
{ std::cout << this << " object::mem_fun_foo(" << a << ',' << b << ")\n" ; }
};
void non_member_bar( object& obj, int a, double b )
{ std::cout << "non_member_bar(" << std::addressof(obj) << ',' << a << ',' << b << ")\n" ; }
void non_member_baz( object* obj, int a, double b ) { non_member_bar(*obj,a,b) ; }
template < typename SEQ, typename FN, typename... ARGS >
void invoke_for_each( SEQ& seq, FN fn, ARGS&&... args )
{
for( auto&& obj : seq )
std::invoke( std::forward<FN>(fn), obj, std::forward<ARGS>(args)... ) ;
}
int main()
{
std::vector<object> vec(3) ;
std::list< std::reference_wrapper<object> > lst( vec.begin(), vec.end() ) ;
std::deque<object*> deq { std::addressof(vec[0]), std::addressof(vec[1]), std::addressof(vec[2] ) };
invoke_for_each( vec, &object::draw, 1.23 ) ;
std::cout << '\n' ;
invoke_for_each( lst, &object::draw, 1.23 ) ;
std::cout << '\n' ;
invoke_for_each( deq, &object::draw, 1.23 ) ;
std::cout << "--------------------------------\n" ;
invoke_for_each( vec, &object::animate, 567 ) ;
std::cout << '\n' ;
invoke_for_each( lst, &object::animate, 567 ) ;
std::cout << '\n' ;
invoke_for_each( deq, &object::animate, 567 ) ;
std::cout << "--------------------------------\n" ;
invoke_for_each( vec, &object::mem_fun_foo, 1.23, 567 ) ;
std::cout << '\n' ;
invoke_for_each( lst, &object::mem_fun_foo, 1.34, 567 ) ;
std::cout << '\n' ;
invoke_for_each( deq, &object::mem_fun_foo, 1.34, 567 ) ;
std::cout << "--------------------------------\n" ;
invoke_for_each( vec, non_member_bar, 1.23, 567 ) ;
std::cout << '\n' ;
invoke_for_each( lst, non_member_bar, 1.34, 567 ) ;
std::cout << '\n' ;
invoke_for_each( deq, non_member_baz, 1.34, 567 ) ;
}
| |