Learning concept checking the hard way - P1
March 21, 2014
What is a concept in C++?
A concept is a set of requirements (valid expressions, associated types, semantic invariants, complexity guarantees, etc.) that a type must fulfill to be correctly used as arguments in a call to a generic algorithm.
Example: Binary Search Tree
If you write your own generic implementation for a BinarySearchTree.
It should basically work for any type that defines
operator. As long as you have that condition satisfied you can build a binary
search tree for that type. For instance,
< is defined for
However, it’s not defined for
complex numbers. C++ has no explicit mechanism
for representing concepts.
Consider the following possible way you may write your generic Binary Search Tree.
Awesome, so it works for both double and int, since
< is defined for
them. What happens if you try it with complex numbers? let’s see:
If you try to run your BinarySearchTree implementation for
numbers you will get a compile time error. So the question is how do you
check if a class has a certain method defined or not?
Above Example is a trivial example to show you the use case, the error
could be buried deep into your code and may not represent the actual reason for
Boost documentation gives a good example.
In short it would be good to have a
has_less method for a generic type
that determines if the given type has a
< method defined or not. Using
this method you can give user a more meaningful error.
Before we proceed, note that you can use concept checking provided by boost library. You’d write something like this:
For more info: visit https://www.boost.org/doc/libs/1_55_0/libs/concept_check/using_concept_check.htm
In the next post, we’ll implement our own version of concept checking. It involves some really cool template tricks and C++ trivia.
Feel free to comment and give suggestions.