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
|
#include <iostream>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/for_each.hpp>
struct print_it
{
template< typename T > void operator() ( T v ) const
{ std::cout << v << ' ' ; }
};
struct sum_it
{
explicit sum_it( int& r ) : sum(r) {}
template< typename T > void operator() ( T v ) const { sum += v ; }
int& sum ;
};
struct A
{
const int arr[10] = { 12, 34, 56, 78, 91, 23, 45, 67, 89, 11 } ;
void print() const { for( int v : arr ) print_it()(v) ; }
int sum() const { int s = 0 ; sum_it fn(s) ; for( int v : arr ) fn(v) ; return s ; }
};
struct B
{
using arr = boost::mpl::vector_c<int, 12, 34, 56, 78, 91, 23, 45, 67, 89, 11 > ;
void print() const { boost::mpl::for_each<arr>( print_it() ) ; }
int sum() const { int s = 0 ; boost::mpl::for_each<arr>( sum_it(s) ) ; return s ; }
};
int main()
{
std::cout << "sizeof(A): " << sizeof(A) << '\n'
<< "sizeof(B): " << sizeof(B) << '\n' ;
A a ; a.print() ; std::cout << " sum: " << a.sum() << '\n' ;
B b ; b.print() ; std::cout << " sum: " << b.sum() << '\n' ;
}
|