Features of C++ Template Metaprograms
Norbert Pataki, Jozsef Mihalicza, Zalan SzugyiViktor Majer, Zoltan Porkolab
[email protected], [email protected], [email protected]@gmail.com, [email protected]
Department of Programming Languages and CompilersEotvos Lorand University, Faculty of Informatics
Pazmany Peter setany 1/C H-1117 Budapest, Hungary
Introduction
Template metaprogramming (TMP) is an emerging new direction inC++ programming for executing algorithms at compilation time ina functional way. Metaprograms are widely used for the followingpurposes:
• optimizing runtime programs (e.g. expression templates)
• enforcing certain semantic checks
• emitting error messages and warnings
• implementing active libraries
Some features:
• all metaprograms rely on the C++ template mechanism
• with templates we can implement recursions and conditionalstatements
• this also means that TMP is Turing-complete.
Unfortunately, template metaprogramming is not yet supported withsufficient software tools (e.g. debugger, profiler, etc.). Our posterintroduces our tools that based on C++ template metaprogramming:
• Templight, a template debugging and profiling framework
• Sophisticated Access Control which makes C++ safer and moreexpressive
Architecture of Templight
original source
preprocessor
#line filter
unaltered preprocessed sourcewithout #line directives
annotator
instrumentator
compiler
warning parser
position adjust
visualiser
trace file
compilation output
instrumented code
annotation
file and line mapping
preprocessed source
line mapping
position correct trace file
Profiling
Templight also can be used profiling template metaprograms.
The hereinafter figure shows that separating precompiler taskschanges the compilation times significantly.
in one steppreprocessedpreprocessing
We measure some well-known metaprogramming utility library. Thefollowing table show the compilation time per template instances:
Template instance Timeiterator_facade<boost::filesystem::bas... 511detail::iterator_facade_types<const st... 420detail::facade_iterator_category_impl<... 290wave::context<char *,Templight::FileAn... 180Templight::Grammar<wave::pp_iterator<b... 171wave::util::functor_input::inner<boost... 161wave::impl::pp_iterator_functor<boost:... 151wave::util::macromap<boost::wave::cont... 130spirit::tree_match<boost::wave::cpplex... 91mpl::if_<boost::detail::is_iterator_ca... 80detail::operator_brackets_result<boost... 71mpl::if_<boost::detail::use_operator_b... 71detail::is_pod_impl<const std::basic_s... 61detail::is_scalar_impl<const std::basi... 51mpl::if_<boost::is_convertible<std::bi... 50mpl::if_<boost::mpl::and_<boost::is_re... 50spirit::unary<boost::spirit::chlit<cha... 50spirit::unary<boost::spirit::chlit<wch... 50spirit::unary<boost::spirit::strlit<co... 50spirit::tree_node<boost::spirit::node_... 41
The following table presents the compilation time per namespaces:
Namespace Time Countboost 4663 819std 772 241Templight 181 5
The following table presents the compilation times per templates:
Template Time Cntiterator_facade 511 1detail::iterator_facade_types 420 1mpl::if_ 411 12detail::facade_iterator_category_impl 290 1detail::is_convertible_impl_dispat... 200 8call_traits 190 5spirit::unary 190 4wave::util::functor_input::inner 181 2wave::context 180 1Templight::Grammar 171 1wave::impl::pp_iterator_functor 151 1std::allocator 131 64wave::util::macromap 130 1detail::is_abstract_imp 120 8detail::is_pointer_impl 110 6spirit::tree_match 91 1detail::is_convertible_impl 80 24detail::operator_brackets_result 71 1detail::is_pod_impl 61 5
Sophisticated Access Control
Modern languages have different approaches to access control, butin statically typed object-oriented languages the simple public-protected-private categorization is the most common. Unfortunately,the expressive power of this categorization is not sufficient for fine-grained access control, since services are exported to clients in ananonymous way.
PRI PUB PRO PAC FRI SEL OBJSimula + + + - - - -
Java + + + + - - -C# + + + + - - -
C++ + + + - + - -D + + + + + - -
Eiffel + + + - - + +
Consider the following example:
class Person{public:
string name; // public for Allprivate:
string address; // public for Pal,Bankstring nickname;// public for Palstring accno; // public for Bankstring dreams; // public for None
};
class Bank { };class Pal { };
We can write the following to express our access rules more pre-cisely:
class Person{public:// accessible for all:
string& name();// accessible from Pal or Bank:accessible InType<_, Typelist(Pal,Bank)>
string& address();// accesible from Pal:accessible SameType<_,Pal>
string& nickname();// accesible from Bank:accessible SameType<_,Bank>
string& accno();private:
string& dreams(); // not accessible
string name_;string address_;string nickname_;string accno_;string dreams_;
};
If the access rules is violated the compilation results in an error mes-sage.
Future Work
Future work includes developing an advanced exception handlingfor C++ which able to follow the thrown and catched exception atcompilation time. Nowadays in C++ this is a runtime activity.
Another direction is to develop a standard testing framework with thehelp of C++ template metaprograms. This means we define a speci-fication language to describe the behaviour of runtime functions andat compilation we generate metaprograms from the specification. Atruntime it can be checked if the result of the specification and thefunction is same.
Debugger Debugger References
[1] Zoltan Porkolab, Jozsef Mihalicza, Adam Sipos: DebuggingC++ Template Metaprograms, Generative Programming and Com-ponent Engineering, The ACM Digital Library, pp. 255–264.
[2] Zoltan Porkolab, Jozsef Mihalicza, Norbert Pataki, Adam Sipos:Towards Profiling C++ Template Metaprograms, In Proc. of the 10thSymposium on Programming Languages and Software Tools, pp.96–111
[3] Jozsef Mihalicza, Norbert Pataki, Zoltan Porkolab, Adam Sipos:Towards More Sophisticated Access Control, In Proc. of the 11thSymposium on Programming Languages and Software Tools, pp.117–131