This example defines a class hierarchy that allows a sequence to be formatted
in several different ways. We'd like to be able to handle any sequence and
any stream type, since the range formatting is independent of the formatting
of individual elements. Thus, our interface needs to look something like
this:

Unfortunately, this is illegal because a virtual function cannot be a template.
However, we can define a class with much the same behavior using Boost.TypeErasure.

#include<boost/type_erasure/any.hpp>#include<boost/type_erasure/iterator.hpp>#include<boost/type_erasure/operators.hpp>#include<boost/type_erasure/tuple.hpp>#include<boost/type_erasure/same_type.hpp>#include<boost/range/begin.hpp>#include<boost/range/end.hpp>#include<boost/range/iterator.hpp>#include<iostream>#include<iomanip>#include<vector>usingnamespaceboost::type_erasure;struct_t:placeholder{};struct_iter:placeholder{};struct_os:placeholder{};template<classT,classU=_self>structbase_and_derived{staticT&apply(U&arg){returnarg;}};namespaceboost{namespacetype_erasure{template<classT,classU,classBase>structconcept_interface<base_and_derived<T,U>,Base,U>:Base{operatortypenamerebind_any<Base,constT&>::type()const{returncall(base_and_derived<T,U>(),const_cast<concept_interface&>(*this));}operatortypenamerebind_any<Base,T&>::type(){returncall(base_and_derived<T,U>(),*this);}};}}// abstract_printer - An abstract base class for formatting sequences.classabstract_printer{public:// print - write a sequence to a std::ostream in a manner// specific to the derived class.//// Requires: Range must be a Forward Range whose elements can be// printed to os.template<classCharT,classTraits,classRange>voidprint(std::basic_ostream<CharT,Traits>&os,constRange&r)const{// Capture the argumentstypenameboost::range_iterator<constRange>::typefirst(boost::begin(r)),last(boost::end(r));tuple<requirements,_os&,_iter,_iter>args(os,first,last);// and forward to the real implementationdo_print(get<0>(args),get<1>(args),get<2>(args));}virtual~abstract_printer(){}protected:// define the concept requirements of the arguments of// print and typedef the any types.typedefboost::mpl::vector<base_and_derived<std::ios_base,_os>,ostreamable<_os,_t>,ostreamable<_os,constchar*>,forward_iterator<_iter,const_t&>,same_type<_t,forward_iterator<_iter,const_t&>::value_type>>requirements;typedefboost::type_erasure::any<requirements,_os&>ostream_type;typedefboost::type_erasure::any<requirements,_iter>iterator_type;// do_print - This method must be implemented by derived classesvirtualvoiddo_print(ostream_typeos,iterator_typefirst,iterator_typelast)const=0;};// separator_printer - writes the elements of a sequence// separated by a fixed string. For example, if// the separator is ", " separator_printer produces// a comma separated list.classseparator_printer:publicabstract_printer{public:explicitseparator_printer(conststd::string&sep):separator(sep){}protected:virtualvoiddo_print(ostream_typeos,iterator_typefirst,iterator_typelast)const{if(first!=last){os<<*first;++first;for(;first!=last;++first){os<<separator.c_str()<<*first;}}}private:std::stringseparator;};// column_separator_printer - like separator_printer, but// also inserts a line break after every n elements.classcolumn_separator_printer:publicabstract_printer{public:column_separator_printer(conststd::string&sep,std::size_tnum_columns):separator(sep),cols(num_columns){}protected:virtualvoiddo_print(ostream_typeos,iterator_typefirst,iterator_typelast)const{std::size_tcount=0;for(;first!=last;++first){os<<*first;boost::type_erasure::any<requirements,_iter>temp=first;++temp;if(temp!=last){os<<separator.c_str();}if(++count%cols==0){os<<"\n";}}}private:std::stringseparator;std::size_tcols;};// aligned_column_printer - formats a sequence in columns// reading down. For example, given the sequence// { 1, 2, 3, 4, 5 }, aligned_column_printer might print// 1 4// 2 5// 3classaligned_column_printer:publicabstract_printer{public:aligned_column_printer(std::size_tcolumn_width,std::size_tnum_columns):width(column_width),cols(num_columns){}protected:virtualvoiddo_print(ostream_typeos,iterator_typefirst,iterator_typelast)const{if(first==last)return;std::vector<iterator_type>column_iterators;// find the tops of the columnsstd::size_tcount=0;for(iterator_typeiter=first;iter!=last;++iter){++count;}std::size_trows=(count+cols-1)/cols;count=0;for(iterator_typeiter=first;iter!=last;++iter){if(count%rows==0){column_iterators.push_back(iter);}++count;}iterator_typelast_col=column_iterators.back();// print the full rowswhile(column_iterators.back()!=last){for(std::vector<iterator_type>::iteratoriter=column_iterators.begin(),end=column_iterators.end();iter!=end;++iter){static_cast<std::ios_base&>(os).width(width);os<<**iter;++*iter;}os<<"\n";}// print the rows that are missing the last columncolumn_iterators.pop_back();if(!column_iterators.empty()){while(column_iterators.back()!=last_col){for(std::vector<iterator_type>::iteratoriter=column_iterators.begin(),end=column_iterators.end();iter!=end;++iter){static_cast<std::ios_base&>(os).width(width);os<<**iter;++*iter;}os<<"\n";}}}private:std::size_twidth;std::size_tcols;};intmain(){inttest[]={1,2,3,4,5,6,7,8,9,10};separator_printerp1(",");p1.print(std::cout,test);std::cout<<std::endl;column_separator_printerp2(",",4);p2.print(std::cout,test);std::cout<<std::endl;aligned_column_printerp3(16,4);p3.print(std::cout,test);}

This example uses C++11 features. You'll need a recent compiler for it
to work.

#include<boost/type_erasure/builtin.hpp>#include<boost/type_erasure/operators.hpp>#include<boost/type_erasure/any_cast.hpp>#include<boost/type_erasure/any.hpp>#include<boost/mpl/vector.hpp>#include<boost/io/ios_state.hpp>#include<iostream>#include<sstream>#include<iomanip>#include<vector>#include<string>namespacempl=boost::mpl;usingnamespaceboost::type_erasure;usingnamespaceboost::io;// We capture the arguments by reference and require nothing// except that each one must provide a stream insertion operator.typedefany<mpl::vector<typeid_<>,ostreamable<>>,const_self&>any_printable;typedefstd::vector<any_printable>print_storage;// Forward declaration of the implementation functionvoidprint_impl(std::ostream&os,constchar*format,constprint_storage&args);// print//// Writes values to a stream like the classic C printf function. The// arguments are formatted based on specifiers in the format string,// which match the pattern://// '%' [ argument-number '$' ] flags * [ width ] [ '.' precision ] [ type-code ] format-specifier//// Other characters in the format string are written to the stream unchanged.// In addition the sequence, "%%" can be used to print a literal '%' character.// Each component is explained in detail below//// argument-number:// The value must be between 1 and sizeof... T. It indicates the// index of the argument to be formatted. If no index is specified// the arguments will be processed sequentially. If an index is// specified for one argument, then it must be specified for every argument.//// flags:// Consists of zero or more of the following:// '-': Left justify the argument// '+': Print a plus sign for positive integers// '0': Use leading 0's to pad instead of filling with spaces.// ' ': If the value doesn't begin with a sign, prepend a space// '#': Print 0x or 0 for hexadecimal and octal numbers.//// width:// Indicates the minimum width to print. This can be either// an integer or a '*'. an asterisk means to read the next// argument (which must have type int) as the width.//// precision:// For numeric arguments, indicates the number of digits to print. For// strings (%s) the precision indicates the maximum number of characters// to print. Longer strings will be truncated. As with width// this can be either an integer or a '*'. an asterisk means// to read the next argument (which must have type int) as// the width. If both the width and the precision are specified// as '*', the width is read first.//// type-code:// This is ignored, but provided for compatibility with C printf.//// format-specifier:// Must be one of the following characters:// d, i, u: The argument is formatted as a decimal integer// o: The argument is formatted as an octal integer// x, X: The argument is formatted as a hexadecimal integer// p: The argument is formatted as a pointer// f: The argument is formatted as a fixed point decimal// e, E: The argument is formatted in exponential notation// g, G: The argument is formatted as either fixed point or using// scientific notation depending on its magnitude// c: The argument is formatted as a character// s: The argument is formatted as a string//template<class...T>voidprint(std::ostream&os,constchar*format,constT&...t){// capture the argumentsprint_storageargs={any_printable(t)...};// and forward to the real implementationprint_impl(os,format,args);}// This overload of print with no explicit stream writes to std::cout.template<class...T>voidprint(constchar*format,constT&...t){print(std::cout,format,t...);}// The implementation from here on can be separately compiled.// utility function to parse an integerintparse_int(constchar*&format){intresult=0;while(charch=*format){switch(ch){case'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':result=result*10+(ch-'0');break;default:returnresult;}++format;}returnresult;}// printf implementationvoidprint_impl(std::ostream&os,constchar*format,constprint_storage&args){intidx=0;ios_flags_saversavef_outer(os,std::ios_base::dec);boolhas_positional=false;boolhas_indexed=false;while(charch=*format++){if(ch=='%'){if(*format=='%'){os<<'%';continue;}ios_flags_saversavef(os);ios_precision_saversavep(os);ios_fill_saversavefill(os);intprecision=0;boolpad_space=false;boolpad_zero=false;// parse argument indexif(*format!='0'){inti=parse_int(format);if(i!=0){if(*format=='$'){idx=i-1;has_indexed=true;++format;}else{os<<std::setw(i);has_positional=true;gotoparse_precision;}}else{has_positional=true;}}else{has_positional=true;}// Parse format modifierswhile((ch=*format)){switch(ch){case'-':os<<std::left;break;case'+':os<<std::showpos;break;case'0':pad_zero=true;break;case' ':pad_space=true;break;case'#':os<<std::showpoint<<std::showbase;break;default:gotoparse_width;}++format;}parse_width:intwidth;if(*format=='*'){++format;width=any_cast<int>(args.at(idx++));}else{width=parse_int(format);}os<<std::setw(width);parse_precision:if(*format=='.'){++format;if(*format=='*'){++format;precision=any_cast<int>(args.at(idx++));}else{precision=parse_int(format);}os<<std::setprecision(precision);}// parse (and ignore) the type modifierswitch(*format){case'h':++format;if(*format=='h')++format;break;case'l':++format;if(*format=='l')++format;break;case'j':case'L':case'q':case't':case'z':++format;break;}std::size_ttruncate=0;// parse the format codeswitch(*format++){case'd':case'i':case'u':os<<std::dec;break;case'o':os<<std::oct;break;case'p':case'x':os<<std::hex;break;case'X':os<<std::uppercase<<std::hex;break;case'f':os<<std::fixed;break;case'e':os<<std::scientific;break;case'E':os<<std::uppercase<<std::scientific;break;case'g':break;case'G':os<<std::uppercase;break;case'c':case'C':break;case's':case'S':truncate=precision;os<<std::setprecision(6);break;default:assert(!"Bad format string");}if(pad_zero&&!(os.flags()&std::ios_base::left)){os<<std::setfill('0')<<std::internal;pad_space=false;}if(truncate!=0||pad_space){// These can't be handled by std::setw. Write to a stringstream and// pad/truncate manually.std::ostringstreamoss;oss.copyfmt(os);oss<<args.at(idx++);std::stringdata=oss.str();if(pad_space){if(data.empty()||(data[0]!='+'&&data[0]!='-'&&data[0]!=' ')){os<<' ';}}if(truncate!=0&&data.size()>truncate){data.resize(truncate);}os<<data;}else{os<<args.at(idx++);}// we can't have both positional and indexed arguments in// the format string.assert(has_positional^has_indexed);}else{std::cout<<ch;}}}intmain(){print("int: %d\n",10);print("int: %0#8X\n",0xA56E);print("double: %g\n",3.14159265358979323846);print("double: %f\n",3.14159265358979323846);print("double: %+20.9e\n",3.14159265358979323846);print("double: %0+20.9g\n",3.14159265358979323846);print("double: %*.*g\n",20,5,3.14159265358979323846);print("string: %.10s\n","Hello World!");print("double: %2$*.*g int: %1$d\n",10,20,5,3.14159265358979323846);}

This example implements an extension of Boost.Function that supports multiple
signatures.

Note

This example uses C++11 features. You'll need a recent compiler for it
to work.

#include<boost/type_erasure/any.hpp>#include<boost/type_erasure/builtin.hpp>#include<boost/type_erasure/callable.hpp>#include<boost/mpl/vector.hpp>#include<boost/variant.hpp>#include<boost/phoenix/core.hpp>#include<boost/phoenix/operator.hpp>#include<boost/range/algorithm.hpp>#include<algorithm>#include<vector>#include<string>#include<iostream>namespacempl=boost::mpl;usingnamespaceboost::type_erasure;namespacephoenix=boost::phoenix;// First of all we'll declare the multifunction template.// multifunction is like Boost.Function but instead of// taking one signature, it takes any number of them.template<class...Sig>usingmultifunction=any<mpl::vector<copy_constructible<>,typeid_<>,relaxed,callable<Sig>...>>;// Let's use multifunction to process a variant. We'll start// by defining a simple recursive variant to use.typedefboost::make_recursive_variant<int,double,std::string,std::vector<boost::recursive_variant_>>::typevariant_type;typedefstd::vector<variant_type>vector_type;// Now we'll define a multifunction that can operate// on the leaf nodes of the variant.typedefmultifunction<void(int),void(double),void(std::string)>function_type;classvariant_handler{public:voidhandle(constvariant_type&arg){boost::apply_visitor(impl,arg);}voidset_handler(function_typef){impl.f=f;}private:// A class that works with boost::apply_visitorstructdispatcher:boost::static_visitor<void>{// used for the leavestemplate<classT>voidoperator()(constT&t){f(t);}// For a vector, we recursively operate on the elementsvoidoperator()(constvector_type&v){boost::for_each(v,boost::apply_visitor(*this));}function_typef;};dispatcherimpl;};intmain(){variant_handlerx;x.set_handler(std::cout<<phoenix::val("Value: ")<<phoenix::placeholders::_1<<std::endl);x.handle(1);x.handle(2.718);x.handle("The quick brown fox jumps over the lazy dog.");x.handle(vector_type{1.618,"Gallia est omnis divisa in partes tres",42});}