C++ Core Guidelines: Other Template Rules

Today, I write about the few remaining rules to templates. Because a collective name is missing, they put the heterogenous rules to templates in the section other. The rules are about best practices but also about surprises. 

 maki 3295891 1280

Here are the rules for this post.

 The first rule is about best practices.

T.140: Name all operations with potential for reuse

Honestly, I'm not so sure why this rule belongs to templates. Maybe templates are about reuse or the example in the guidelines uses the std::find_if algorithm of the Standard Template Library. Anyway, the rule is fundamental from the code quality perspective.

Imagine you have a vector of records. Each record consists of a name, an address, and an id. Quite often, you want to find a record with a specific name; but to make it more challenging, you ignore the case-sensitivity of the names.


// records.cpp

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>

struct Rec {                                                     // (1)
    std::string name;
    std::string addr;
    int id;         

int main(){
    std::cout << std::endl;
    std::vector<Rec> vr{ {"Grimm", "Munich", 1},                 // (2)
                         {"huber", "Stuttgart", 2},
                         {"Smith", "Rottenburg", 3},
                         {"black", "Hanover", 4} };
    std::string name = "smith";
    auto rec = std::find_if(vr.begin(), vr.end(), [&](Rec& r) {  // (3)
        if (r.name.size() != name.size()) return false;            
        for (int i = 0; i < r.name.size(); ++i){                   
            if (std::tolower(r.name[i]) != std::tolower(name[i])) return false;
        return true;                                               
    if (rec != vr.end()){
        std::cout << rec->name << ",  " << rec->addr << ", " << rec->id << std::endl;
    std::cout << std::endl;


The struct Rec (line 1) has only public members; therefore, I can use aggregate initialisation and initialise all members directly in line (2). In line (3) I use a lambda function to search for the record with the name "smith". First, I check if both names have the same size and second if the characters are identical when compared case-insensitive.  


What's the problem with the code? The requirement of the case-insensitive comparison of strings is too common, and we should, therefore, put the solution in an object, give it a name and reuse it.

bool compare_insensitive(const std::string& a, const std::string& b)    // (1)
    if (a.size() != b.size()) return false;
    for (int i = 0; i < a.size(); ++i){
        if (std::tolower(a[i]) != std::tolower(b[i])) return false;
    return true;

std::string name = "smith";

auto res = std::find_if(vr.begin(), vr.end(),         
    [&](Rec& r) { compare_insensitive(r.name, name); }

std::vector<std::string> vs{"Grimm", "huber", "Smith", "black"};        // (2)

auto res2 = std::find_if(vs.begin(), vs.end(),
    [&](std::string& r) { compare_insensitive(r, name); }


The function compare_insensitive (line 1) gives the general concept a name. Now,  I can use it for a vector of strings (line 2).

T.141: Use an unnamed lambda if you need a simple function object in one place only

Admittely, I have often this discussion in my classes: When should I use a function (function object) or a lambda function? Honstly, I have no easy answer. Here, two meta-rules of code quality contradict:

  1. Don't repeat yourself. (DRY)
  2. Explicit is better than implicit. (The Zen of Python)

Sorry, I borrowed the second point form Python. But what does that mean. Imagine, you have an old-fashioned Fortran programmer in your team and he says to you: "Each name must have three characters." So, you end with the following code.

auto eUE = std::remove_if(use.begin(), use.end(), igh);           


What does the name igh stand for? igh stands for a id greater hundred. Now, you are forced to document the usage of the predicate.

But If you use a lambda function the code documents itself.

auto earlyUsersEnd = std::remove_if(users.begin(), users.end(),
                                    [](const User &user) { return user.id > 100; });                    

Believe me; I had discussions with Fortran programmers about names. Admittedly, more arguments such as code locality versus code size speaks for or againts lambda functions but "Don't repeat yourself" versus "Explicit is better than implicit" are my key arguments.

T.143: Don’t write unintentionally nongeneric code

A short example says more than a long explanation. In the following example, I iterate through a std::vector, a std::deque, and a std::list.

// notGeneric.cpp

#include <deque>
#include <list>
#include <vector>

template <typename Cont>
void justIterate(const Cont& cont){
    const auto itEnd = cont.end();
    for (auto it = cont.begin(); it < itEnd; ++it) {    // (1)
        // do something
int main(){
    std::vector<int> vecInt{1, 2, 3, 4, 5};
    justIterate(vecInt);                                // (2)
    std::deque<int> deqInt{1, 2, 3, 4, 5};
    justIterate(deqInt);                                // (3)
    std::list<int> listInt{1, 2, 3, 4, 5};
    justIterate(listInt);                               // (4)


The code looks innocent but when I want to compile the program, the compilation breaks. I get about 100 lines of error messages.


The beginning of the error message you see that it is quite precise: "notGeneric.cpp:10:37: error: no match for 'operator<' (operand types are 'std::_List_const_iterator".

What is the issue? The issue is in line (1). The iterator comparison (<) works for the std::vector (line 2) and the std::deque (line 3) but breaks for the std::list (line 4). Each container returns an iterator representing its structure. This is in case of a std::vector and a std::deque a random access iterator and in case of the std::list a bidirectional iterator. A look at the iterator categories helps a lot.


The random access iterator category is a superset of the bidirectional iterator category and the bidirectional iterator category is a superset of the forward iterator category. Now, the issue is obvious. An iterator given by a list does not support the smaller comparison. Fixing the bug is quite easy. Iterators of each iterator category support the != comparison. Here is the fixed justIterate function template.


template <typename Cont>
void justIterate(const Cont& cont){
    const auto itEnd = cont.end();
    for (auto it = cont.begin(); it != itEnd; ++it) {   // (1)
        // do something


By the way, it is typically a bad idea to loop through a container such as I do it in the function justIterate. This is a job for an appropriate algorithm of the standard template library.

What's next?

My original plan was it to write today also about the rule T.144: Don’t specialize function templates. This rule holds a big surprise potential. You will see what I mean in the next post.

Thanks a lot to my Patreon Supporters: Paul Baxter,  Meeting C++, Matt Braun, Avi Lachmish, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Dilettant, Marko, Ramesh Jangama, G Prvulovic, Reiner Eiteljörge.

Thanks in particular to:  TakeUpCode 450 60


Get your e-book at Leanpub:

The C++ Standard Library


Concurrency With Modern C++


Get Both as one Bundle

cover   ConcurrencyCoverFrame   bundle
With C++11, C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.  

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.


Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 600 pages full of modern C++ and more than 100 source files presenting concurrency in practice.


Get your interactive course


Modern C++ Concurrency in Practice

C++ Standard Library including C++14 & C++17

educative CLibrary

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.

What's Inside?

  • 140 lessons
  • 110 code playgrounds => Runs in the browser
  • 78 code snippets
  • 55 illustrations

Based on my book "The C++ Standard Library" educative.io created an interactive course.

What's Inside?

  • 149 lessons
  • 111 code playgrounds => Runs in the browser
  • 164 code snippets
  • 25 illustrations
Tags: templates

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 2167

Yesterday 7957

Week 34689

Month 189284

All 4810178

Currently are 236 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments