Under Construction – Begin
From my “Printing Standard Library Sequence and Associative Containers in C++ 2011″ post, let’s discuss how to print Tuples:
template < size_t i, size_t n, typename ... T_PACK > struct std__tuple { static inline void write ( std::ostream & o, std::tuple< T_PACK ... > const & t ) { o << ", " << std::get< i >( t ); std__tuple< i + 1, n, T_PACK ... >::write( o, t ); } }; template < typename ... T_PACK > struct std__tuple< 0, 0, T_PACK ... > { static inline void write ( std::ostream &, std::tuple< T_PACK ... > const & ) { // } }; template < size_t n, typename ... T_PACK > struct std__tuple< 0, n, T_PACK ... > { static inline void write ( std::ostream & o, std::tuple< T_PACK ... > const & t ) { o << ' ' << std::get< 0 >( t ); std__tuple< 1, n, T_PACK ... >::write( o, t ); } }; template < size_t n, typename ... T_PACK > struct std__tuple< n, n, T_PACK ... > { static inline void write ( std::ostream & o, std::tuple< T_PACK ... > const & ) { o << ' '; } }; template < typename ... T_PACK > inline std::ostream & operator << ( std::ostream & o, std::tuple< T_PACK ... > const & t ) { o << '{'; std__tuple< 0, sizeof...( T_PACK ), T_PACK ... >::write( o, t ); o << '}'; return o; }
Now, a tuple is neither a Sequence nor an Associative Container. In fact, a tuple isn’t even in the Containers Library section of the C++ 2011 Standard (Clause 23). Tuples are listed in Clause 20.4 . . . right after Pairs (Clause 20.3). Per 20.4.1[1], “tuples are heterogeneous, fixed-size collections of values.” Moreover, “an instantiation of tuple with two arguments is similar to an instantiation of pair with the same two arguments.” Unlike Containers that require “insert and erase operations” per 23.2.1[1] so that they can shrink and grow at run-time, tuples are fixed at compile-time . . . and since they’re fixed at compile-time, we’ll need to use a little bit of the power of the dark side, template meta-programming.
NOTE: In C++ 2011, some of the functionality associated with template meta-programming in C++ 1998/2003 has been replaced by Constant Expressions (Clause 5.19) . . . and Constant Expression Programming. __Some__ of the functionality, but not __all__. Wherever possible, use Constant Expression Programming, vice template meta-programming, since Constant Expressions were made from the ground up for compile-time programming … while being able to program with templates is just a side-effect of how templates are instantiated; moreover, template meta-programming was discovered to exist after the machinery of template instantiation was put in place … so the syntax is anything but user-friendly.
That being said, Constant Expression Programming doesn’t work here … or at least, I haven’t managed to get it to work here.