1
Features of C++ Template Metaprograms Norbert Pataki, J´ ozsef Mihalicza, Zal´ an Sz ˝ ugyi Viktor M´ ajer, Zolt´ an Porkol´ ab [email protected], [email protected], [email protected] [email protected], [email protected] Department of Programming Languages and Compilers otv¨ os Lor´ and University, Faculty of Informatics azm´ any P´ eter s´ et´ any 1/C H-1117 Budapest, Hungary Introduction Template metaprogramming (TMP) is an emerging new direction in C++ programming for executing algorithms at compilation time in a functional way. Metaprograms are widely used for the following purposes: 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 conditional statements this also means that TMP is Turing-complete. Unfortunately, template metaprogramming is not yet supported with sufficient software tools (e.g. debugger, profiler, etc.). Our poster introduces our tools that based on C++ template metaprogramming: Templight, a template debugging and profiling framework Sophisticated Access Control which makes C++ safer and more expressive Architecture of Templight original source preprocessor #line filter unaltered preprocessed source without #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 tasks changes the compilation times significantly. in one step preprocessed preprocessing We measure some well-known metaprogramming utility library. The following table show the compilation time per template instances: Template instance Time iterator_facade<boost::filesystem::bas... 511 detail::iterator_facade_types<const st... 420 detail::facade_iterator_category_impl<... 290 wave::context<char * ,Templight::FileAn... 180 Templight::Grammar<wave::pp_iterator<b... 171 wave::util::functor_input::inner<boost... 161 wave::impl::pp_iterator_functor<boost:... 151 wave::util::macromap<boost::wave::cont... 130 spirit::tree_match<boost::wave::cpplex... 91 mpl::if_<boost::detail::is_iterator_ca... 80 detail::operator_brackets_result<boost... 71 mpl::if_<boost::detail::use_operator_b... 71 detail::is_pod_impl<const std::basic_s... 61 detail::is_scalar_impl<const std::basi... 51 mpl::if_<boost::is_convertible<std::bi... 50 mpl::if_<boost::mpl::and_<boost::is_re... 50 spirit::unary<boost::spirit::chlit<cha... 50 spirit::unary<boost::spirit::chlit<wch... 50 spirit::unary<boost::spirit::strlit<co... 50 spirit::tree_node<boost::spirit::node_... 41 The following table presents the compilation time per namespaces: Namespace Time Count boost 4663 819 std 772 241 Templight 181 5 The following table presents the compilation times per templates: Template Time Cnt iterator_facade 511 1 detail::iterator_facade_types 420 1 mpl::if_ 411 12 detail::facade_iterator_category_impl 290 1 detail::is_convertible_impl_dispat... 200 8 call_traits 190 5 spirit::unary 190 4 wave::util::functor_input::inner 181 2 wave::context 180 1 Templight::Grammar 171 1 wave::impl::pp_iterator_functor 151 1 std::allocator 131 64 wave::util::macromap 130 1 detail::is_abstract_imp 120 8 detail::is_pointer_impl 110 6 spirit::tree_match 91 1 detail::is_convertible_impl 80 24 detail::operator_brackets_result 71 1 detail::is_pod_impl 61 5 Sophisticated Access Control Modern languages have different approaches to access control, but in 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 an anonymous way. PRI PUB PRO PAC FRI SEL OBJ Simula + + + - - - - Java + + + + - - - C# + + + + - - - C++ + + + - + - - D + + + + + - - Eiffel + + + - - + + Consider the following example: class Person { public: string name; // public for All private: string address; // public for Pal,Bank string nickname;// public for Pal string accno; // public for Bank string 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 handling for C++ which able to follow the thrown and catched exception at compilation time. Nowadays in C++ this is a runtime activity. Another direction is to develop a standard testing framework with the help of C++ template metaprograms. This means we define a speci- fication language to describe the behaviour of runtime functions and at compilation we generate metaprograms from the specification. At runtime it can be checked if the result of the specification and the function is same. Debugger Debugger References [1] Zolt´ an Porkol´ ab, J´ ozsef Mihalicza, ´ Ad´ am Sipos: Debugging C++ Template Metaprograms, Generative Programming and Com- ponent Engineering, The ACM Digital Library, pp. 255–264. [2] Zolt´ an Porkol´ ab, J ´ ozsef Mihalicza, Norbert Pataki, ´ Ad´ am Sipos: Towards Profiling C++ Template Metaprograms, In Proc. of the 10th Symposium on Programming Languages and Software Tools, pp. 96–111 [3] J ´ ozsef Mihalicza, Norbert Pataki, Zolt´ an Porkol´ ab, ´ Ad´ am Sipos: Towards More Sophisticated Access Control, In Proc. of the 11th Symposium on Programming Languages and Software Tools, pp. 117–131

Features of C++ Template Metaprogramspatakino.web.elte.hu/ICAI_Poster.pdfFeatures of C++ Template Metaprograms Norbert Pataki, Jozsef Mihalicza, Zal´ an Sz´ ugyi˝ Viktor Majer,

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Features of C++ Template Metaprogramspatakino.web.elte.hu/ICAI_Poster.pdfFeatures of C++ Template Metaprograms Norbert Pataki, Jozsef Mihalicza, Zal´ an Sz´ ugyi˝ Viktor Majer,

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