Op 05-Mar-17 om 14:09 schreef Alvin:
> I'm wondering if there is a more elegant way to write the following code
> in C++14. The purpose is to duplicate a set of statements (fully unroll
> a loop). The loop index isn't required.
This seemed to work for me (at least with the higher optimization settings):
template< int N >
struct loop_unrolled{
template< typename Body >
loop_unrolled( const Body & body ){
body();
loop_unrolled< N - 1 > dummy( body );
}
};
template<>
struct loop_unrolled< 0 >{
template< typename Body >
loop_unrolled( const Body & body ){}
};
int n = 0;
loop_unrolled< 8 >( [&]{
std::cout << static_cast( n++ )<< " ";
});
see
http://www.voti.nl/blog/?p=81
Wouter "Objects? No Thanks!" van Ooijen