How to avoid errors while using CRTP?
Using CRTP sometimes I write a code like this:
// this was written first
struct Foo : Base<Foo, ...>
{
...
};
// this was copy-pasted from Foo some days later
struct Bar : Base<Foo, ...>
{
...
};
And it's very difficult to understand what goes wrong, until I trace code in debugger and see that Bar's members aren't used in Base
.
How to reveal this error at compile time?
(I use MSVC2010, so I can use some C++0x features and MSVC language extensions)
In C++0x you have a simple solution. I don't know whether it is implemented in MSVC10 however.
template <typename T>
struct base
{
private:
~base() {}
friend T;
};
// Doesn't compile (base class destructor is private)
struct foo : base<bar> { ... };
You can use something like this:
template<class T> class Base {
protected:
// derived classes must call this constructor
Base(T *self) { }
};
class Foo : public Base<Foo> {
public:
// OK: Foo derives from Base<Foo>
Foo() : Base<Foo>(this) { }
};
class Moo : public Base<Foo> {
public:
// error: constructor doesn't accept Moo*
Moo() : Base<Foo>(this) { }
};
class Bar : public Base<Foo> {
public:
// error: type 'Base<Bar>' is not a direct base of 'Bar'
Bar() : Base<Bar>(this) { }
};
template<typename T, int arg1, int arg2>
struct Base
{
typedef T derived_t;
};
struct Foo : Base<Foo, 1, 2>
{
void check_base() { Base::derived_t(*this); } // OK
};
struct Bar : Base<Foo, 1, 2>
{
void check_base() { Base::derived_t(*this); } // error
};
This code is based on Amnon's answer, but checking code don't contains name of derived class, so I can copy and paste it without changes.