使用CRTP有时我会编写如下代码:
// this was written first
struct Foo : Base<Foo, ...>
{
...
};
// this was copy-pasted from Foo some days later
struct Bar : Base<Foo, ...>
{
...
};
在我在调试器中跟踪代码并看到Bar的成员未在Base
中使用之前,很难理解出现了什么问题。
如何在编译时显示此错误?
(我使用MSVC2010,所以我可以使用一些C ++ 0x功能和MSVC语言扩展)
答案 0 :(得分:13)
在C ++ 0x中,您有一个简单的解决方案。我不知道它是否在MSVC10中实现。
template <typename T>
struct base
{
private:
~base() {}
friend T;
};
// Doesn't compile (base class destructor is private)
struct foo : base<bar> { ... };
答案 1 :(得分:10)
您可以使用以下内容:
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) { }
};
答案 2 :(得分:2)
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
};
此代码基于Amnon's answer,但检查代码不包含派生类的名称,因此我可以复制并粘贴它而无需更改。
答案 3 :(得分:0)
无法知道派生类型。您可以强制执行从Foo
派生的Base<Foo>
,但您不能强制执行其他类也不会从中获取。
答案 4 :(得分:0)
我可以使用宏
#define SOMENAMESPACE_BASE(type, arg1, arg2) type : Base<type, arg1, arg2>
但如果存在更好的解决方案,我不想使用宏。