воскресенье, 11 декабря 2011 г.

Type Traits. Functions

Got a sneak peek on boost::function_traits implementation. One more partial template specialization trick:
Specialized member_function struct declarations splits original "typename T" into three separate template arguments allowing us to decompose member function reference into return type, object type and argument type. boost::function_traits implementation is a bit more generalized, supports for multiple arguments, but the code above can be quite useful too though.

суббота, 10 декабря 2011 г.

Type Traits. Pointers

I have not read Alexandrescu’s “Modern C++” entirely. The beginning is quite fascinating but as you go deeper you have strong feeling that you will never going need it or that someone will kill you for “template magic”. But... Currently I’m trying to write convenient API and found myself looking for useful tips inside “Modern C++”.

The trick above is about determining if ‘U’ is a pointer type or not. Code is based on common technique of using partially specialized templates - “struct is_pointr” is more concreate specialization so “struct is_pointer” will be used only in case of non pointer “U”. You can find that “is_pointer” is already part of boost’s type traits and std technical report 1, therefore code may be rewritten to shift responsibility for strange templates from you