Skip to Main Content
Advanced Metaprogramming in Classic C++
book

Advanced Metaprogramming in Classic C++

by Davide Di Gennaro
May 2015
Intermediate to advanced content levelIntermediate to advanced
572 pages
9h 52m
English
Apress
Content preview from Advanced Metaprogramming in Classic C++

CHAPTER 5

image

Interfaces

Templates are used as interfaces in two different ways: to provide sets of atomic functions and to obtain compile-time polymorphism.

If several functions use the same portion of the interface of an object, you can factor them out in a single template:

void do_something(std::vector<double>& v){   if (v.empty())      // ...   ... v.size();   for_each(v.begin(), v.end(), my_functor());   ...}void do_something(std::list<double>& L){   if (L.empty())      // ...   ... L.size();   for_each(L.begin(), L.end(), my_functor());   ...}

becomes:

template <typename T>void do_something(T& L){   if (L.empty())      // ...   ... L.size(); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical C++ Metaprogramming

Practical C++ Metaprogramming

Edouard Alligand, Joel Falcou
Data Parallel C++: Mastering DPC++ for Programming of Heterogeneous Systems using C++ and SYCL

Data Parallel C++: Mastering DPC++ for Programming of Heterogeneous Systems using C++ and SYCL

James Reinders, Ben Ashbaugh, James Brodman, Michael Kinsner, John Pennycook, Xinmin Tian
Open Source Software Superstream Series: C++

Open Source Software Superstream Series: C++

Kelsey Hightower, Matt Klein, Alex Gallego, Timur Doumler, Daisy Hollman, Sy Brand, Rob Blafford, Vadim Plakhtinskiy

Publisher Resources

ISBN: 9781484210109Purchase book