C++ Core Guidelines: Other Template Rules

C++ Core Guidelines: Other Template Rules

This is a cross-post from www.ModernesCpp.com.

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. 

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 it structure. This is in case for 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.

Bad example for - "T.143: Don’t write unintentionally nongeneric code". Why do you think that void justIterate(const Cont& cont) needs to work for all iterators? Maybe it was written with the idea to work only for random access iterators and all it needs is a static_assert inside so that it will brake complication for std::list with a human readable error. ?

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了