C++ Core Guidelines: Other Template Rules

Contents[Show]

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). Inline (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.  

 records

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 a 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

Admittedly, I have often this discussion in my classes: When should I use a function (function object) or a lambda function? Honestly, 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 from 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 speak 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.

notGeneric

At 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 the case of a std::vector and a std::deque a random access iterator and in the case of the std::list a bidirectional iterator. A look at the iterator categories helps a lot.

IteratorCategories

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 to write today also about 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: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, and Evangelos Denaxas.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

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

Visitors

Today 1409

Yesterday 7507

Week 24540

Month 84169

All 6312641

Currently are 380 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments