Presentation is loading. Please wait.

Presentation is loading. Please wait.

GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers.

Similar presentations


Presentation on theme: "GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers."— Presentation transcript:

1 GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers Eötvös Loránd University, Budapest, Hungary {scamel, gsd}@elte.hu

2 GPCE'04, Vancouver2 C++ Templates Provides language support for parametric polymorphism Provides language support for parametric polymorphism “Type-aware smart macros” “Type-aware smart macros” Many checks are delayed until instantiation Many checks are delayed until instantiation No restrictions on parameters No restrictions on parameters Templates alone form a Turing-complete functional language inside C++ Templates alone form a Turing-complete functional language inside C++ Based on template specializations Based on template specializations

3 GPCE'04, Vancouver3 Issues on Templates Lazy evaluation: instantiate template members only on request Lazy evaluation: instantiate template members only on request Wider scale of parameters supported Wider scale of parameters supported Avoid code bloat Avoid code bloat Unexpected late errors Unexpected late errors Hard to decode error messages Hard to decode error messages Classic example: Classic example: Member function of a class template using srot() instead of sort() compiles Member function of a class template using srot() instead of sort() compiles Compile error when calling member function Compile error when calling member function

4 GPCE'04, Vancouver4 Introspection Entity can observe its own state Entity can observe its own state Information gained in runtime from: Information gained in runtime from: Interpreter (Perl, Ruby, etc) Interpreter (Perl, Ruby, etc) VM (Java,.Net, etc) VM (Java,.Net, etc) Environment (C++ RTTI) Environment (C++ RTTI) Information gained from compiler Information gained from compiler Direct language support Direct language support Hand written exploits Hand written exploits

5 GPCE'04, Vancouver5 Introspection in C++ Limited direct support in the language Limited direct support in the language sizeof sizeof Would be useful to Would be useful to Make restrictions on template parameters (concept checking) Make restrictions on template parameters (concept checking) Extend current type system (generate conversions, operations) Extend current type system (generate conversions, operations) Optimization (e.g. expression templates) Optimization (e.g. expression templates)

6 GPCE'04, Vancouver6 Compile-time Adaptation A program can observe its state A program can observe its state May make decisions depending on state May make decisions depending on state Data structures Data structures Implementation strategies Implementation strategies Template metaprogramming, e.g. boost::mpl Template metaprogramming, e.g. boost::mpl E.g. a container decide to store: E.g. a container decide to store: Comparable values in binary tree Comparable values in binary tree Other values in list Other values in list Compiler rules apply for generated program Compiler rules apply for generated program

7 GPCE'04, Vancouver7 Concept Checking Concept: list of requirements on a type Concept: list of requirements on a type Enforce concepts on template parameters immediately at instantiation Enforce concepts on template parameters immediately at instantiation Improve implementation quality Improve implementation quality Especially useful at the STL, e.g. for iterator categories Especially useful at the STL, e.g. for iterator categories

8 GPCE'04, Vancouver8 Concept Checking in Other Languages Parameter type must implement interface or derive from base class Parameter type must implement interface or derive from base class Java Java interface Sortable {... } class SortedList... Eiffel Eiffel class SORTED_LIST [T -> COMPARABLE]... Similar in functional languages Similar in functional languages

9 GPCE'04, Vancouver9 Concept Checking in Other Languages Ada: generic interface, no inheritance required Ada: generic interface, no inheritance requiredgeneric --- Element type --- Element type type T is private; type T is private; --- Comparision on element type --- Comparision on element type with function “ ; with function “ ; package Sorted_List......end --- Generics are instantiated explicitly package SL is new Sorted_List(MyType, “<=“);

10 GPCE'04, Vancouver10 Concept Checking Techniques Currently used in C++: Currently used in C++: require (GNU STL) require (GNU STL) boost::concept_check boost::concept_check Shortages Shortages No elementary conditions identified No elementary conditions identified No orthogonality No orthogonality No composition: No composition: “Fulfill or die” philosophy “Fulfill or die” philosophy Implicit conjunction Implicit conjunction No cooperation with other libraries No cooperation with other libraries

11 GPCE'04, Vancouver11 Ideal Concept Checking Library Compile time “execution” Compile time “execution” Identifying elementary conditions Identifying elementary conditions Orthogonal design Orthogonal design Boolean results instead of aborting compilation Boolean results instead of aborting compilation Condition composition Condition composition Cooperation with metaprogramming libraries Cooperation with metaprogramming libraries Something like an extension to boost::type_traits for concept checking Something like an extension to boost::type_traits for concept checking

12 GPCE'04, Vancouver12 Goals Implement introspection library to enable concept checking that is close to ideal Implement introspection library to enable concept checking that is close to ideal Cooperation with metaprogramming libraries (e.g. boost::mpl ) Cooperation with metaprogramming libraries (e.g. boost::mpl ) Use only standard C++ Use only standard C++ No language extension No language extension No typeof operator No typeof operator No compiler-specific features No compiler-specific features No external tools (e.g. gcc-xml ) No external tools (e.g. gcc-xml )

13 GPCE'04, Vancouver13 Programming Techniques Ellipse: accept any parameter Ellipse: accept any parameter void f(...); Function overload Function overload Yes isOk(ExpectedType); // expected case No isOk(...); // rescue case Return types have different sizes Return types have different sizes struct No { char dummy; }; struct Yes { char dummy[2]; };

14 GPCE'04, Vancouver14 Programming Techniques Map return types to boolean values Map return types to boolean values sizeof( isOk(expression) ) == sizeof(Yes); Convenience macro for mapping Convenience macro for mapping // --- The same with macro shortcut bool res = CONFORMS( isOk(expression) );

15 GPCE'04, Vancouver15 Programming Techniques boost::enable_if boost::enable_if // --- Enabled case template template struct enable_if { typedef T Result; }; // --- Specialized disabled case template template struct enable_if {};

16 GPCE'04, Vancouver16 Programming Techniques SFINAE rule (Substitution failure is not an error) SFINAE rule (Substitution failure is not an error) // --- Expected case for T enable_if f(T&); // --- Rescue case No f(...); bool res = CONFORMS( f(Type()) );

17 GPCE'04, Vancouver17 Elementary Conditions Checks implemented for Checks implemented for Existence of embedded type with name Existence of embedded type with name Existence of class member with name Existence of class member with name Exact type of embedded type Exact type of embedded type Exact type of member (both function and data) Exact type of member (both function and data)

18 GPCE'04, Vancouver18 Embedded Type Name Inspect if type has an embedded type with given name Inspect if type has an embedded type with given name Need macros Need macrosPREPARE_TYPE_CHECKER(iterator);... // --- Look for name ‘iterator’ in T const bool res = CONFORMS( TYPE_IN_CLASS(iterator, T) );

19 GPCE'04, Vancouver19 Member Name Inspect if type has a non-type member (either data or function) with given name Inspect if type has a non-type member (either data or function) with given name Similar to previous condition Similar to previous conditionPREPARE_MEMBER_CHECKER(size);... // --- Look for member ‘size’ in T const bool res = CONFORMS( MEMBER_IN_CLASS(size, T) );

20 GPCE'04, Vancouver20 Nested Type Inspect nested type if name exists Inspect nested type if name exists Cooperate with other tools, e.g. boost::type_traits Cooperate with other tools, e.g. boost::type_traits // --- Inspect traits of nested type const bool isScalar = type_traits ::is_scalar; // --- Inspect exact type const bool isInteger = SameTypes ::Result;

21 GPCE'04, Vancouver21 Members Exact type of member if name exists Exact type of member if name exists // --- Inspect if ‘size’ is data member const bool isDataMember = CONFORMS( Member ::NonStatic( &T::size ) ); // --- Inspect if ‘size’ is member function typedef unsigned int Fun() const; const bool isMemberFunction = CONFORMS( Member ::NonStatic( &T::size ) );

22 GPCE'04, Vancouver22 Composite Conditions User can easily assemble complex conditions User can easily assemble complex conditions template struct LessThanComparable { enum { Conforms = CONFORMS( Function :: Static( &::operator :: Static( &::operator< )|| CONFORMS( Function :: NonStatic( &T::operator :: NonStatic( &T::operator< )};};

23 GPCE'04, Vancouver23 Hacking? Two opinions: Two opinions: Should be language extension Should be language extension Should be user code Should be user code Language change Language change Major issue Major issue Must have proved design Must have proved design Should be based on previous experiences Should be based on previous experiences

24 GPCE'04, Vancouver24 Open Questions What is the minimal orthogonal set of conditions? What is the minimal orthogonal set of conditions? What other conditions are expected to be implemented? What other conditions are expected to be implemented? What conditions are theoretically impossible to implement with current language standard? What conditions are theoretically impossible to implement with current language standard? DefaultConstructable ? DefaultConstructable ?

25 GPCE'04, Vancouver25 Summary Separation of introspection from intercession Separation of introspection from intercession Implemented elementary conditions Implemented elementary conditions Observation provides compile time bool Observation provides compile time bool Easy to define new user conditions Easy to define new user conditions Arbitrary combination of conditions is possible Arbitrary combination of conditions is possible And, or, negate And, or, negate Specialization on this result is possible Specialization on this result is possible Supports compile-time adaptation Supports compile-time adaptation Supports concept checking Supports concept checking

26 GPCE'04, Vancouver26 Further Plans Implement more elementary conditions Implement more elementary conditions Check theorethical limitation of implementation Check theorethical limitation of implementation Implement a concept-checking library usable in the STL Implement a concept-checking library usable in the STL

27 GPCE'04, Vancouver 27 Thank You! See related material at http://gsd.web.elte.hu


Download ppt "GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers."

Similar presentations


Ads by Google