使用CRTP时如何避免错误?

发布于 2024-10-07 07:35:56 字数 440 浏览 0 评论 0原文

使用 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语言扩展)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

帝王念 2024-10-14 07:35:56

在 C++0x 中,您有一个简单的解决方案。我不知道它是否在MSVC10中实现。

template <typename T>
struct base
{
private:
    ~base() {}
    friend T;
};

// Doesn't compile (base class destructor is private)
struct foo : base<bar> { ... };

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> { ... };
狼性发作 2024-10-14 07:35:56

你可以使用这样的东西:

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) { }
};

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) { }
};
清引 2024-10-14 07:35:56
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 的回答,但检查代码不包含派生类的名称,因此我可以复制并粘贴它而无需更改。

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.

半暖夏伤 2024-10-14 07:35:56

无法知道派生类型。您可以强制要求 Foo 派生自 Base,但不能强制要求没有其他类也从中派生。

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.

听风念你 2024-10-14 07:35:56

我可以使用宏

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

,但如果存在更好的解决方案,我不想使用宏。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文