Home > Frameworks > boost::concept_check


One of the big problem with templates is that not every data type is always compatible with every templates, and when trying to unfold a template with an incompatible data type, compilers tends to generate very cryptic, hard to understand error messages.  That’s where boost::concept_check comes to the rescue.

The concept_check framework provides a compile-time assert system that spew easy to read error messages if a template is trying to be generated from an incompatible type.

The template needs to implement a set of “requirements” for the type(s), so it’s not completelly automatized.  But the interface is easy enough to understand and implement.

Here’s an easy test I did. First, the headers:

#include "boost/concept/assert.hpp"
#include "boost/concept_check.hpp"

I want to see if a specific type is comparable (that is, two objects of that type can be compared with == operator).  So my first test was to try it on a simple integer:


My code build fine.  Obviously, integers can be compared together.  Now I created a dummy class with no support for the == operator:

class MyClass {int var;};

In this case if I try to compile:


I get an error message specifying that MyClass does not support comparison.

This is just a simple example with the “Comparable<>” test, but there’s a wide array of requirement tests provided by the framework, that you can find here.

The framework also provide a macro to group all requirements in a single block with BOOST_CONCEPT_USAGE & BOOST_CONCEPT_REQUIRES, but I haven’t dug too much into them for the time being.

Categories: Frameworks
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: