, . , , .
++ 11. ++ 1y auto: ++ 11 void.
functor :
struct example_functor {
template<unsigned N>
static void action(double d) const {
std::cout << N << ":" << d << "\n";
}
};
++ 11 :
template<unsigned...> struct indexes {};
template<unsigned Max, unsigned... Is> struct make_indexes:make_indexes< Max-1, Max-1, Is... > {};
template<unsigned... Is> struct make_indexes<0, Is...>:indexes<Is...> {};
.
:
template<typename Functor, unsigned Max, typename... Ts>
void invoke_jump( unsigned index, Ts&&... ts );
:
invoke_jump<example_functor, 10>( 7, 3.14 );
:
template<typename Functor, unsigned... Is, typename... Ts>
void do_invoke_jump( unsigned index, indexes<Is...>, Ts&&... ts ) {
static auto table[]={ &(Functor::template action<Is>)... };
table[index]( std::forward<Ts>(ts)... )
}
template<typename Functor, unsigned Max, typename... Ts>
void invoke_jump( unsigned index, Ts&&... ts ) {
do_invoke_jump( index, make_indexes<Max>(), std::forward<Ts>(ts)... );
}
static Functor::action, .
++ 03 ..., , . std::vector.
, Functor.action<I>() [, ] :
template<unsigned Begin, unsigned End, typename Functor>
struct ForEach:ForEach<Begin, End-1, Functor> {
ForEach(Functor& functor):
ForEach<Begin, End-1, Functor>(functor)
{
functor->template action<End-1>();
}
};
template<unsigned Begin, typename Functor>
struct ForEach<Begin,Begin,Functor> {};
, ( ).
vector .
template<typename Signature, typename Functor>
struct PopulateVector {
std::vector< Signature* >* target;
PopulateVector(std::vector< Signature* >* t):target(t) {}
template<unsigned I>
void action() {
target->push_back( &(Functor::template action<I>) );
}
};
:
template<typename Signature, typename Functor, unsigned Max>
std::vector< Signature* > make_table() {
std::vector< Signature* > retval;
retval.reserve(Max);
PopulateVector<Signature, Functor> worker(&retval);
ForEach<0, Max>( worker );
return retval;
}
std::vector.
Ith .
struct example_functor {
template<unsigned I>
static void action() {
std::cout << I << "\n";
}
};
void test( unsigned i ) {
static std::vector< void(*)() > table = make_table< void(), example_functor, 100 >();
if (i < 100)
table[i]();
}
, i, , .
, , , i . action static, , static, .
++ 03 , , ( std::vector ).
scanline, , - scanline. , 1 .
- : .