Cpp Core Guidelines: Type Erasure

Cpp Core Guidelines: Type Erasure

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

Rule "T.5: Combine generic and OO techniques to amplify their strengths, not their costs" of the core guidelines to generic programming uses type erasure as an example. Type erasure? Really! Of course, it takes me two posts to explain this advanced template technique.

First of all: What does type erasure mean?

  • Type Erasure: Type Erasure enables you to use various concrete types through a single generic interface.

Of course, you already quite often used type erasure in C++ or C. The C-ish way of type erasure is a void pointer; the C++-ish way of type erasure is object-orientation. Let's start with a void pointer.

Void Pointer

Let's have a closer look at the declaration of std::qsort:

void qsort(void *ptr, std::size_t count, std::size_t size, cmp);

with:

int cmp(const void *a, const void *b)

The comparison function should return a

  • negative integer: the first argument is less than the second
  • zero: both arguments are equal
  • positive integer: the first argument is greater than the second

Thanks to the void pointer, std::qsort is generally applicable but also quite error-prone.

Maybe you want to sort a std::vector<int>, but you used a comparator for C-strings. The compiler can not catch this error because the type information was removed. You end with undefined behaviour.

In C++ we can do better:

Object Orientation

Here is a simple example, which serves as a starting point for further variations. 

// typeErasureOO.cpp

#include <iostream>
#include <string>
#include <vector>

struct BaseClass{                                       // (2)
	virtual std::string getName() const = 0;
};

struct Bar: BaseClass{                                  // (4)
	std::string getName() const override {
	    return "Bar";
	}
};

struct Foo: BaseClass{                                  // (4)
	std::string getName() const override{
	    return "Foo";
	}
};

void printName(std::vector<const BaseClass*> vec){      // (3)for (auto v: vec) std::cout << v->getName() << std::endl;
}


int main(){
	
	std::cout << std::endl;
	
	Foo foo;
	Bar bar; 
	
	std::vector<const BaseClass*> vec{&foo, &bar};   // (1)
	
	printName(vec);
	
	std::cout << std::endl;

}

std::vector<const Base*> (1) has a pointer to a constant BaseClasses. BaseClass is abstract Base Class, which is used in (3). Foo and Bar (4) are the concrete classes.

The output of the program is not so thrilling.

To say it more formally. Foo and Bar implement the interface of the BaseClass and can, therefore, be used instead of BaseClass. This principle is called substitution principle and is type erasure in OO.

In Object Oriented Programming you implement an interface. In dynamically typed languages such as Python, you are not interested in interfaces you are interested in behaviour.

Templates

Let me make a short detour.


In Python, you care about behaviour and not about formal interfaces. This idea is well-known as duck typing. To make it short, the expression goes back to the poem from James Whitcomb Rileys: Here it is:

 

“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”

What does that mean? Imagine a function acceptOnlyDucks that only accepts ducks as an argument. In statically typed languages such as C++, all types which are derived from Duck can be used to invoke the function. In Python, all types, which behave like Duck's, can be used to invoke the function. To make it more concrete. If a bird behaves like a Duck it is a Duck. There is often a proverb used in Python to describe this behaviour quite good.

Don't ask for permission, ask for forgiveness.

In case of our Duck, this means, that you invoke the function acceptsOnlyDucks with a bird and hope for the best. If something bad happens, you catch the exception with an except clause. Often this strategy works very well and very fast in Python.

Okay, this is the end of my detour. Maybe you wonder, why I wrote about duck typing in this C++ post. The reason is quite straightforward. Thanks to templates, we have duck typing in C++. When you combine duck typing together with OO, it becomes even type safe.

std: as a polymorphic function wrapper is a nice example to type erasure in C++.

std::function

std::function can accept everything, which behaves like a function. To be more precise. This can be any callable such as a function, a function object, a function object created by std::bind , or just a lambda function.

// callable.cpp

#include <cmath>
#include <functional>
#include <iostream>
#include <map>

double add(double a, double b){
	return a + b;
}

struct Sub{
	double operator()(double a, double b){
		return a - b;
	}
};

double multThree(double a, double b, double c){
	return a * b * c;
}

int main(){
    
    using namespace std::placeholders;

    std::cout << std::endl;

    std::map<const char , std::function<double(double, double)>> dispTable{  // (1)
        {'+', add },                                         // (2)
        {'-', Sub() },                                       // (3)
        {'*', std::bind(multThree, 1, _1, _2) },             // (4)
        {'/',[](double a, double b){ return a / b; }}};      // (5)

    std::cout << "3.5 + 4.5 = " << dispTable['+'](3.5, 4.5) << std::endl;
    std::cout << "3.5 - 4.5 = " << dispTable['-'](3.5, 4.5) << std::endl;
    std::cout << "3.5 * 4.5 = " << dispTable['*'](3.5, 4.5) << std::endl;
    std::cout << "3.5 / 4.5 = " << dispTable['/'](3.5, 4.5) << std::endl;

    std::cout << std::endl;

}

I use in this example a dispatch table (1) which maps characters to callables. A callable can be a function (1), a function object (2), a function object created by std::bind (3), or a lambda function. The key point of std::function is, that it accepts all different function types and erasure their types. std::function requires from its callables that takes two double's and return a double: std::function<double(double, double)>.

To complete the example, here is the output.

Before I write in the next post more about type erasure with templates, let me summarise the three techniques to implement type erasure.

You can implement type erasure with void-pointers, object-orientation, or templates. Only the implementation with templates is type safe and don't require a type hierarchy. The missing details to templates will follow.

What's next?

I assume, you want to know, how type erasure with templates is implemented? Of course, you have to wait for my next post.

 

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

社区洞察

其他会员也浏览了