C++ Core Guidelines: Rules for Templates and Hierarchies

Contents[Show]

Due to the  C++ core guidelines "Templates are the backbone of C++’s support for generic programming and class hierarchies the backbone of its support for object-oriented programming. The two language mechanisms can be used effectively in combination, but a few design pitfalls must be avoided." Let me see what this means.

 

hierarchy 35795 1280

This section consists of five rules.

Rule T.81 is too loosely related to templates and hierarchies, and rule T.82 is empty; therefore, my post boils down to the three remaining rules.

I will write about the rules T.80 and T.84 together because T.84 continues the story of T.80.

 

T.80: Do not naively templatize a class hierarchy and T.84: Use a non-template core implementation to provide an ABI-stable interface

Here is as an example a naively templatized class hierarchy from the guidelines:

 

template<typename T>
struct Container {         // an interface
    virtual T* get(int i);
    virtual T* first();
    virtual T* next();
    virtual void sort();
};

template<typename T>
class Vector : public Container<T> {
public:
    // ...
};

Vector<int> vi;
Vector<string> vs;

 

Why is this due to the guidelines naively? This is in particular naively because the base class Container has many virtual functions. The presented design introduces code bloat. Virtual functions are instantiated every time in a class template. In contrast, non-virtual functions are only instantiated if they are used.

A simple test with CppInsight proves my point.

Non-virtual functions

The following program uses a std::vector<int> and a std::vector<std::string>.

Vector1

CppInsight shows it. No method of std::vector is instantiated.

Vector2

Virtual functions

Here is the simplified program of the C++ core guidelines including the virtual function sort.

Vector1Virtual

Now, the virtual function sort is instantiated. Here is only the output of CppInsight which shows the instantiation of the class template Container.

Vector2Virtual

 

In total, I get 100 lines of code in the case of the virtual function.

The note to the guidelines gives a hint on how to overcome this code bloat. Often you can provide a stable interface by not parameterising a base. This brings me to the related rule T.84: Use a non-template core implementation to provide an ABI-stable interface.

Okay, I already have written about this technique in the post C++ Core Guidelines: Template Definitions. Rule T.84 mentions an alternative way to address a stable interface: Pimpl.

Pimpl

Pimpl stands for "pointer to implementation" and means to remove implementation details of a class by placing them in a separate class, accessed through a pointer. This technique should be in the toolbox of each serious C++ programmer. Pimpl is often also called compilation firewall because this technique breaks the dependency between the implementation and the users of the class interface. This means the implementation can be changed without recompiling the user code.

Here is the general structure from Herb Sutters Blog:  GotW #100: Compilation Firewalls.

 

// in header file
class widget {
public:
    widget();
    ~widget();
private:
    class impl;                                         // (1)
    unique_ptr<impl> pimpl;
};
 
// in implementation file                               // (2)                 
class widget::impl {
    // :::
};
 
widget::widget() : pimpl{ new impl{ /*...*/ } } { }
widget::~widget() { }  

 

This are the points of this idiom.

  1. put all private non-virtual members into impl (1)
  2. forward declare impl
  3. define impl in the corresponding implementation file (2)

Okay. Let me show a full example based on the one from cppreference.com.

 

// pimpl.cpp

#include <iostream>
#include <memory>
 
// interface (widget.h)
class widget {
    class impl;
    std::unique_ptr<impl> pImpl;
 public:
    void draw();
    bool shown() const { return true; } 
    widget(int);
    ~widget(); 
    widget(widget&&) = default;  
    widget(const widget&) = delete;
    widget& operator=(widget&&); 
    widget& operator=(const widget&) = delete;
};
 
// implementation (widget.cpp)
class widget::impl {
    int n; // private data
 public:
    void draw(const widget& w) {                             // (1)
        if(w.shown())
            std::cout << "drawing a widget " << n << '\n';
    }
    impl(int n) : n(n) {}
};
void widget::draw() { pImpl->draw(*this); }                  // (2)
widget::widget(int n) : pImpl{std::make_unique<impl>(n)} {}
widget::~widget() = default;                                 // (3)
widget& widget::operator=(widget&&) = default;
 
// user (main.cpp)
int main()
{
    std::cout << std::endl;
	
    widget w(7);
    w.draw();
	
    std::cout << std::endl;
   
}

 

The draw call of the class impl uses a back-reference to widget (lines (1) and (2)). The destructor and the move assignment operator (line (3)) must be user-defined in the implementation file base because std::unique_ptr requires that the pointed-to type is a complete type.

The program behaves as expected:

pimpl

Besides its pros, the pimpl idiom has two cons: there is an additional pointer indirection, and you have to store the pointer.

The last guideline for this post is about a typical misconception.

T.83: Do not declare a member function template virtual

Let me try to use a virtual member function template.

 

// virtualMember.cpp

class Shape {
    template<class T>
    virtual bool intersect(T* p);
};

int main(){
    
    Shape shape;

}

 

 The error message from my GCC 8.2 compiler is crystal-clear:

virtualMember

What's next?

The next guidelines and, therefore, my next post is about variadic templates. Variadic templates are templates that can accept an arbitrary number of arguments. The rules in the guidelines to variadic templates as many rules to templates consist only of the headings. This means I write in my next post more general about variadic templates.

 

 

 

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, 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, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Satish Vangipuram, and Michael Dunsky.

 

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

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. 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

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 5241

Yesterday 11310

Week 27525

Month 188199

All 6836891

Currently are 154 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments