문제

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.

There's no way of knowing the deriving type. You could enforce that Foo derived from Base<Foo>, but you can't enforce that no other classes also derive from that.

I can use a macro

#define SOMENAMESPACE_BASE(type, arg1, arg2) type : Base<type, arg1, arg2>

but I don't want to use macros if better solution exists.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top