SlideShare a Scribd company logo
Arindam Mukherjee 
Pune C++ and Boost Meetup 
C++TEMPLATE METAPROGRAMMING
Template type computations 
 Templates provide a Turing complete 
computation subsystem – that runs during 
compilation. 
 This capability was not entirely consciously 
designed. 
 Erwin Unruh found this somewhat 
accidentally ~ early 90s.
Templates 
 Compile time entities completely evaluated 
by the compiler, based on arguments passed. 
std::vector<int> nums; // std::vector<int> unrelated 
std::vector<std::string> names; // to std::vector<std::string> 
template <typenameT> class Foo; // declaration 
template <typenameT> class Foo { … }; // definition 
Foo<int> foonum; // Foo<int> is specialization
Non-type template parameters 
 Templates can have non-type parameters: 
template <typename T, int N> 
struct array { 
T data[N]; 
…. 
} 
 Non-type args are compile time constants. 
array<int, 10> arr; // array of 10 ints 
 sizeof is evaluated at compile time. 
int arr[sizeof(int)]; // array of 4* ints
Embedded types 
 Typedefs inside a class / class template. 
template <typename T> 
struct add_const { 
typedef const T type; 
}; 
add_const<int>::type ci; // const int
Embedded constants 
 Enums inside a class / class template. 
template <int M, int N> struct add { 
enum { value = M + N } 
} 
int array[add<10, 20>::value];
Branching: specializations 
 Specialize for specific types. 
template <typename T> 
struct Foo { … }; // default impl 
template <> 
struct Foo<int> { … }; // int-specific 
 Applicable to function templates too.
Partial specializations 
 Specialize for type families. 
template <typename T, typename U> 
struct Foo { … }; // default impl 
template <typename T> 
struct Foo<T, T> { … }; // Foo<char, char> 
template <typename T> 
struct Foo<T*, T> { … }; // Foo<int*, int> 
 NOT applicable to function templates.
Recursion by specializations 
 Compute factorial at compile time: 
template <int N> struct factorial { 
enum { value = N*factorial<N-1>::value }; 
}; 
template <> struct factorial<0> { // 
terminating condition 
enum { value = 1 }; 
};
Applying the techniques 
 Remove const: 
template <typename T> struct remove_const 
{ typedef T type; }; 
template <typename T> // partial 
struct remove_const<const T> { // speclzn 
typedef T type; 
}; 
 Called as: 
remove_const<const int>::type x; // int
Applying the techniques 
 Querying types: 
template <typename T, typename U> 
struct are_same 
{ enum { value = 0 }; }; 
template <typename T> / partial 
struct are_same<T, T> // specialization 
{ enum { value = 1 }; }; 
 Called as: 
are_same<int, int>::value == 1;
Applying the techniques 
 Remove levels of indirection: 
template <typename T> 
struct deref 
{ typedef T type; }; 
template <typename T> // partial 
struct deref<T*> { // specialization 
typedef typename deref<T>::type type; 
} 
 Called as: 
deref<int*****>::type x; // x is int
Boost Type Traits 
 A header-only library to query and 
manipulate types at compile time. 
 Lots of trait metafunctions. 
#include <boost/type_traits.hpp> 
typedef int* intptr; 
assert(boost::is_ptr<intptr>::value); 
struct Foo {int a; void *b; float c; }; 
assert(boost::is_pod<Foo>::value);
SFINAE 
 Compiler tries to resolve function calls to a 
unique overload, or unique template with 
appropriate arguments. 
 Multiple templates may be candidates but a 
unique one must survive. 
 All candidates instantiated. 
 Those that fail instantiation eliminated. Not 
An Error if a single candidate survives.
SFINAE & enable_if 
 Exploit SFINAE to resolve to apt template. 
 boost::enable_if 
#include <boost/utility/enable_if.hpp> 
boost::enable_if<true, T>::type x; // T 
boost::enable_if<false, T>::type y; //!def 
 boost::disable_if (opposite of enable_if).
SFINAE & enable_if (contd.) 
 Serialize arbitrary types: 
#include <boost/utility/enable_if.hpp> 
typedef vector<char> buffer_t; 
template <typename T> 
buffer_t serialize(const T&); 
 Need a fast version for POD types and 
generic version for non-POD types. 
template <typename T> 
enable_if<is_pod<T>, vector<char>>::type 
serialize(const T&) {…} // pod version 
// Use disable_if for the non-POD version
Metafunctions 
 Metafunction: a class or class template taking 
only type parameters, with a single 
embedded typedef. 
 All the Boost type traits are metafunctions. 
 Boost TMP Library: library of metafunctions. 
 Provides means of composing and generating 
metafunctions from simpler metafunctions.
Composing metafunctions 
 Metafunctions need to be combined: 
#include <boost/type_traits.hpp> 
template <typename T> void foo(T obj) { 
if (boost::is_pointer<T>::value || 
boost::is_array<T>::value) //runtime 
{ … } 
else { … } 
}
Boost MPL (contd.) 
 How to make another metafunction that OR’s 
two type traits. 
#include <boost/mpl/or.hpp> 
#include <boost/type_traits.hpp> 
template <typename T> void foo(T obj) { 
if (boost::mpl::or_< 
boost::is_pointer<T> 
, boost::is_array<T> 
>::value) 
{ … } 
else { … } 
}
Boost MPL (contd.) 
 Numbers can be wrapped in types: 
#include <boost/mpl/integral_c.hpp> 
#include <boost/mpl/greater.hpp> 
#include <boost/type_traits.hpp> 
namespace mpl = boost::mpl; 
template <typename T, typename U> 
struct is_larger : mpl::greater< 
mpl::integral_c<size_t, sizeof T> 
, mpl::integral_c<size_t, sizeof U> 
> 
{};
Thank you! 
 Q & A 
 Book: Modern C++ Design – Andrei 
Alexandrescu 
 Book: Advanced C++ Template 
Metaprogramming – Davide di Gennaro 
 Book: C++ Template Metaprogramming – 
Dave Abrahams, Aleksey Gurtovoy

More Related Content

PPTX
Dissecting ssl threats
Zscaler
 
PPTX
ATT&CKing with Threat Intelligence
Christopher Korban
 
PDF
Michael jackson e o ultimo segredo illuminati
Biblioteca Virtual
 
PDF
ESM 6.5c SP1 ArcSight Web User's Guide
Protect724mouni
 
PDF
Illuminati Provide Funding To Promote Scientific Propaganda
Zurich Files
 
PPT
Satanic Ritual Abuse And The Illuminati Part 1
Mormons4justice
 
PDF
Russian Jews and Gentiles
Zurich Files
 
PDF
Welcome to the Jungle: Pentesting AWS
Mike Felch
 
Dissecting ssl threats
Zscaler
 
ATT&CKing with Threat Intelligence
Christopher Korban
 
Michael jackson e o ultimo segredo illuminati
Biblioteca Virtual
 
ESM 6.5c SP1 ArcSight Web User's Guide
Protect724mouni
 
Illuminati Provide Funding To Promote Scientific Propaganda
Zurich Files
 
Satanic Ritual Abuse And The Illuminati Part 1
Mormons4justice
 
Russian Jews and Gentiles
Zurich Files
 
Welcome to the Jungle: Pentesting AWS
Mike Felch
 

Similar to C++ metaprogramming (20)

PPTX
C++ metaprogramming
Arindam Mukherjee
 
PPTX
C++ Templates. SFINAE
GlobalLogic Ukraine
 
PPT
2CPP15 - Templates
Michael Heron
 
PDF
Programming at Compile Time
emBO_Conference
 
PPT
Lecture 04 - Templates.ppt,Class Templates and Function templates
kaurgurkiranpreet92
 
PDF
C++ Training
SubhendraBasu5
 
PDF
Generic programming and concepts that should be in C++
Anton Kolotaev
 
PPTX
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
PPT
Static and dynamic polymorphism
umesh patil
 
PPT
Static and dynamic polymorphism
sanjay joshi
 
PPTX
Object Oriented Programming using C++: C++ Templates.pptx
RashidFaridChishti
 
PDF
4.1 C++ Template for engineering course. Learn easily
kk6369150094
 
PDF
Modern C++
Michael Clark
 
PPTX
C++ Basics
Himanshu Sharma
 
PDF
Bw14
Hassan62424
 
PPTX
Polymorphism & Templates
Meghaj Mallick
 
PDF
Metaprogramming
Ganesh Samarthyam
 
PPT
Glimpses of C++0x
ppd1961
 
PPTX
The Style of C++ 11
Sasha Goldshtein
 
PPT
templates.ppt
Saiganesh124618
 
C++ metaprogramming
Arindam Mukherjee
 
C++ Templates. SFINAE
GlobalLogic Ukraine
 
2CPP15 - Templates
Michael Heron
 
Programming at Compile Time
emBO_Conference
 
Lecture 04 - Templates.ppt,Class Templates and Function templates
kaurgurkiranpreet92
 
C++ Training
SubhendraBasu5
 
Generic programming and concepts that should be in C++
Anton Kolotaev
 
Whats New in Visual Studio 2012 for C++ Developers
Rainer Stropek
 
Static and dynamic polymorphism
umesh patil
 
Static and dynamic polymorphism
sanjay joshi
 
Object Oriented Programming using C++: C++ Templates.pptx
RashidFaridChishti
 
4.1 C++ Template for engineering course. Learn easily
kk6369150094
 
Modern C++
Michael Clark
 
C++ Basics
Himanshu Sharma
 
Polymorphism & Templates
Meghaj Mallick
 
Metaprogramming
Ganesh Samarthyam
 
Glimpses of C++0x
ppd1961
 
The Style of C++ 11
Sasha Goldshtein
 
templates.ppt
Saiganesh124618
 
Ad

Recently uploaded (20)

PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Presentation about variables and constant.pptx
safalsingh810
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Ad

C++ metaprogramming

  • 1. Arindam Mukherjee Pune C++ and Boost Meetup C++TEMPLATE METAPROGRAMMING
  • 2. Template type computations  Templates provide a Turing complete computation subsystem – that runs during compilation.  This capability was not entirely consciously designed.  Erwin Unruh found this somewhat accidentally ~ early 90s.
  • 3. Templates  Compile time entities completely evaluated by the compiler, based on arguments passed. std::vector<int> nums; // std::vector<int> unrelated std::vector<std::string> names; // to std::vector<std::string> template <typenameT> class Foo; // declaration template <typenameT> class Foo { … }; // definition Foo<int> foonum; // Foo<int> is specialization
  • 4. Non-type template parameters  Templates can have non-type parameters: template <typename T, int N> struct array { T data[N]; …. }  Non-type args are compile time constants. array<int, 10> arr; // array of 10 ints  sizeof is evaluated at compile time. int arr[sizeof(int)]; // array of 4* ints
  • 5. Embedded types  Typedefs inside a class / class template. template <typename T> struct add_const { typedef const T type; }; add_const<int>::type ci; // const int
  • 6. Embedded constants  Enums inside a class / class template. template <int M, int N> struct add { enum { value = M + N } } int array[add<10, 20>::value];
  • 7. Branching: specializations  Specialize for specific types. template <typename T> struct Foo { … }; // default impl template <> struct Foo<int> { … }; // int-specific  Applicable to function templates too.
  • 8. Partial specializations  Specialize for type families. template <typename T, typename U> struct Foo { … }; // default impl template <typename T> struct Foo<T, T> { … }; // Foo<char, char> template <typename T> struct Foo<T*, T> { … }; // Foo<int*, int>  NOT applicable to function templates.
  • 9. Recursion by specializations  Compute factorial at compile time: template <int N> struct factorial { enum { value = N*factorial<N-1>::value }; }; template <> struct factorial<0> { // terminating condition enum { value = 1 }; };
  • 10. Applying the techniques  Remove const: template <typename T> struct remove_const { typedef T type; }; template <typename T> // partial struct remove_const<const T> { // speclzn typedef T type; };  Called as: remove_const<const int>::type x; // int
  • 11. Applying the techniques  Querying types: template <typename T, typename U> struct are_same { enum { value = 0 }; }; template <typename T> / partial struct are_same<T, T> // specialization { enum { value = 1 }; };  Called as: are_same<int, int>::value == 1;
  • 12. Applying the techniques  Remove levels of indirection: template <typename T> struct deref { typedef T type; }; template <typename T> // partial struct deref<T*> { // specialization typedef typename deref<T>::type type; }  Called as: deref<int*****>::type x; // x is int
  • 13. Boost Type Traits  A header-only library to query and manipulate types at compile time.  Lots of trait metafunctions. #include <boost/type_traits.hpp> typedef int* intptr; assert(boost::is_ptr<intptr>::value); struct Foo {int a; void *b; float c; }; assert(boost::is_pod<Foo>::value);
  • 14. SFINAE  Compiler tries to resolve function calls to a unique overload, or unique template with appropriate arguments.  Multiple templates may be candidates but a unique one must survive.  All candidates instantiated.  Those that fail instantiation eliminated. Not An Error if a single candidate survives.
  • 15. SFINAE & enable_if  Exploit SFINAE to resolve to apt template.  boost::enable_if #include <boost/utility/enable_if.hpp> boost::enable_if<true, T>::type x; // T boost::enable_if<false, T>::type y; //!def  boost::disable_if (opposite of enable_if).
  • 16. SFINAE & enable_if (contd.)  Serialize arbitrary types: #include <boost/utility/enable_if.hpp> typedef vector<char> buffer_t; template <typename T> buffer_t serialize(const T&);  Need a fast version for POD types and generic version for non-POD types. template <typename T> enable_if<is_pod<T>, vector<char>>::type serialize(const T&) {…} // pod version // Use disable_if for the non-POD version
  • 17. Metafunctions  Metafunction: a class or class template taking only type parameters, with a single embedded typedef.  All the Boost type traits are metafunctions.  Boost TMP Library: library of metafunctions.  Provides means of composing and generating metafunctions from simpler metafunctions.
  • 18. Composing metafunctions  Metafunctions need to be combined: #include <boost/type_traits.hpp> template <typename T> void foo(T obj) { if (boost::is_pointer<T>::value || boost::is_array<T>::value) //runtime { … } else { … } }
  • 19. Boost MPL (contd.)  How to make another metafunction that OR’s two type traits. #include <boost/mpl/or.hpp> #include <boost/type_traits.hpp> template <typename T> void foo(T obj) { if (boost::mpl::or_< boost::is_pointer<T> , boost::is_array<T> >::value) { … } else { … } }
  • 20. Boost MPL (contd.)  Numbers can be wrapped in types: #include <boost/mpl/integral_c.hpp> #include <boost/mpl/greater.hpp> #include <boost/type_traits.hpp> namespace mpl = boost::mpl; template <typename T, typename U> struct is_larger : mpl::greater< mpl::integral_c<size_t, sizeof T> , mpl::integral_c<size_t, sizeof U> > {};
  • 21. Thank you!  Q & A  Book: Modern C++ Design – Andrei Alexandrescu  Book: Advanced C++ Template Metaprogramming – Davide di Gennaro  Book: C++ Template Metaprogramming – Dave Abrahams, Aleksey Gurtovoy