CRTP 中的模板化派生类(奇怪的重复模板模式)

发布于 2024-09-03 12:02:59 字数 1530 浏览 16 评论 0原文

我使用了不能用 g++ 4.2.1 编译的 CRTP,也许是因为派生类本身就是一个模板?有谁知道为什么这不起作用,或者更好的是,如何使其起作用?示例代码和编译器错误如下。

来源: foo.C

#include <iostream>

using namespace std;

template<typename X, typename D> struct foo;

template<typename X> struct bar : foo<X,bar<X> >
{
  X evaluate() { return static_cast<X>( 5.3 ); }
};

template<typename X> struct baz : foo<X,baz<X> >
{
  X evaluate() { return static_cast<X>( "elk" ); }
};

template<typename X, typename D> struct foo : D
{
  X operator() () { return static_cast<D*>(this)->evaluate(); }
};

template<typename X, typename D>
void print_foo( foo<X,D> xyzzx )
{
  cout << "Foo is " << xyzzx() << "\n";
}

int main()
{
  bar<double> br;
  baz<const char*> bz;

  print_foo( br );
  print_foo( bz );

  return 0;
}

编译器错误

foo.C: In instantiation of ‘foo<double, bar<double> >’:
foo.C:8:   instantiated from ‘bar<double>’
foo.C:30:   instantiated from here
foo.C:18: error: invalid use of incomplete type ‘struct bar<double>’
foo.C:8: error: declaration of ‘struct bar<double>’
foo.C: In instantiation of ‘foo<const char*, baz<const char*> >’:
foo.C:13:   instantiated from ‘baz<const char*>’
foo.C:31:   instantiated from here
foo.C:18: error: invalid use of incomplete type ‘struct baz<const char*>’
foo.C:13: error: declaration of ‘struct baz<const char*>’

I have a use of the CRTP that doesn't compile with g++ 4.2.1, perhaps because the derived class is itself a template? Does anyone know why this doesn't work or, better yet, how to make it work? Sample code and the compiler error are below.

Source: foo.C

#include <iostream>

using namespace std;

template<typename X, typename D> struct foo;

template<typename X> struct bar : foo<X,bar<X> >
{
  X evaluate() { return static_cast<X>( 5.3 ); }
};

template<typename X> struct baz : foo<X,baz<X> >
{
  X evaluate() { return static_cast<X>( "elk" ); }
};

template<typename X, typename D> struct foo : D
{
  X operator() () { return static_cast<D*>(this)->evaluate(); }
};

template<typename X, typename D>
void print_foo( foo<X,D> xyzzx )
{
  cout << "Foo is " << xyzzx() << "\n";
}

int main()
{
  bar<double> br;
  baz<const char*> bz;

  print_foo( br );
  print_foo( bz );

  return 0;
}

Compiler errors

foo.C: In instantiation of ‘foo<double, bar<double> >’:
foo.C:8:   instantiated from ‘bar<double>’
foo.C:30:   instantiated from here
foo.C:18: error: invalid use of incomplete type ‘struct bar<double>’
foo.C:8: error: declaration of ‘struct bar<double>’
foo.C: In instantiation of ‘foo<const char*, baz<const char*> >’:
foo.C:13:   instantiated from ‘baz<const char*>’
foo.C:31:   instantiated from here
foo.C:18: error: invalid use of incomplete type ‘struct baz<const char*>’
foo.C:13: error: declaration of ‘struct baz<const char*>’

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

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

发布评论

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

评论(2

冰雪之触 2024-09-10 12:02:59

有一种方法可以使 CRTP 适用于模板派生类,但需要注意的是它应该始终是模板。

#include <iostream>
#include <typeinfo>


template<template <class> class Derived, class T>
struct A{
    void interface(){
        static_cast<Derived<T>*>(this)->impl();
    }
};

template<class T>
struct B: public A<B, T>
{
    void impl(){
        std::cout << "CRTP with derived templates are real\n";
        std::cout << "Tempate argument is " << typeid(T).name() << "\n";
    }
};

int main(void)
{   
    B<char>().interface();
    B<double>().interface();
    B<void>().interface();
    return 0;
}

适用于 gcc 4.4.7 甚至更旧的版本,无法确定。

There is a way to make CRTP works for a template derived class, with a caveat that it should always be a template.

#include <iostream>
#include <typeinfo>


template<template <class> class Derived, class T>
struct A{
    void interface(){
        static_cast<Derived<T>*>(this)->impl();
    }
};

template<class T>
struct B: public A<B, T>
{
    void impl(){
        std::cout << "CRTP with derived templates are real\n";
        std::cout << "Tempate argument is " << typeid(T).name() << "\n";
    }
};

int main(void)
{   
    B<char>().interface();
    B<double>().interface();
    B<void>().interface();
    return 0;
}

Works with gcc 4.4.7 and maybe older, can't check for sure.

芸娘子的小脾气 2024-09-10 12:02:59

CRTP 的想法是拥有一个知道其派生类是什么类型的基类 - 不要让基类从其派生类派生。
否则,您将遇到以下情况:

  • Derived 派生自 Base,后者
  • 派生自 Derived,后者
  • 又派生于 Base< ;Derived>,其中
  • ...

请改用以下内容:

template<typename X, typename D> struct foo // : D
// ...                                         ^ remove that

The idea of CRTP is to have a base class that knows of what type its derivative is - not to let the base class derive from its derivative.
Otherwise you'd have the following situation:

  • Derived derives from Base<Derived>, which
  • derives from Derived, which
  • derives from Base<Derived>, which
  • ...

Use the following instead:

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