C ++静态多态性(CRTP)并使用派生类中的typedef

我阅读了Wikipedia上有关C ++中反复出现的模板模式的信息,该模板模式用于执行静态(阅读:编译时)多态性。我想对其进行概括,以便可以根据派生类型更改函数的返回类型。(这似乎应该可行,因为基本类型从template参数知道派生类型)。不幸的是,以下代码无法使用MSVC 2010进行编译(我现在无法轻松访问gcc,因此我还没有尝试过)。有人知道为什么吗?


template <typename derived_t>

class base {

public:

    typedef typename derived_t::value_type value_type;

    value_type foo() {

        return static_cast<derived_t*>(this)->foo();

    }

};


template <typename T>

class derived : public base<derived<T> > {

public:

    typedef T value_type;

    value_type foo() {

        return T(); //return some T object (assumes T is default constructable)

    }

};


int main() {

    derived<int> a;

}

顺便说一句,我有一个使用额外模板参数的解决方法,但是我不喜欢它-当在继承链中传递许多类型时,它将变得非常冗长。


template <typename derived_t, typename value_type>

class base { ... };


template <typename T>

class derived : public base<derived<T>,T> { ... };

编辑:


MSVC 2010在这种情况下给出的错误消息是 error C2039: 'value_type' : is not a member of 'derived<T>'


g ++ 4.1.2(通过codepad.org)说error: no type named 'value_type' in 'class derived<int>'


Helenr
浏览 647回答 3
3回答

catspeake

derivedbase在基类列表中将其用作模板参数时,它是不完整的。常见的解决方法是使用特征类模板。这是您的示例,特征化。这显示了如何通过特征使用派生类中的类型和函数。// Declare a base_traits traits class template:template <typename derived_t>&nbsp;struct base_traits;// Define the base class that uses the traits:template <typename derived_t>&nbsp;struct base {&nbsp;&nbsp; &nbsp; typedef typename base_traits<derived_t>::value_type value_type;&nbsp; &nbsp; value_type base_foo() {&nbsp; &nbsp; &nbsp; &nbsp; return base_traits<derived_t>::call_foo(static_cast<derived_t*>(this));&nbsp; &nbsp; }};// Define the derived class; it can use the traits too:template <typename T>struct derived : base<derived<T> > {&nbsp;&nbsp; &nbsp; typedef typename base_traits<derived>::value_type value_type;&nbsp; &nbsp; value_type derived_foo() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return value_type();&nbsp;&nbsp; &nbsp; }};// Declare and define a base_traits specialization for derived:template <typename T>&nbsp;struct base_traits<derived<T> > {&nbsp; &nbsp; typedef T value_type;&nbsp; &nbsp; static value_type call_foo(derived<T>* x) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return x->derived_foo();&nbsp;&nbsp; &nbsp; }};您只需要专门base_traits用于模板参数derived_t的任何类型,base并确保每个专门化都能提供所需的所有成员base。

海绵宝宝撒

使用特征的一个小缺点是您必须为每个派生类声明一个。您可以像这样编写一个不太冗长和繁琐的解决方法:template <template <typename> class Derived, typename T>class base {public:&nbsp; &nbsp; typedef T value_type;&nbsp; &nbsp; value_type foo() {&nbsp; &nbsp; &nbsp; &nbsp; return static_cast<Derived<T>*>(this)->foo();&nbsp; &nbsp; }};template <typename T>class Derived : public base<Derived, T> {public:&nbsp; &nbsp; typedef T value_type;&nbsp; &nbsp; value_type foo() {&nbsp; &nbsp; &nbsp; &nbsp; return T(); //return some T object (assumes T is default constructable)&nbsp; &nbsp; }};int main() {&nbsp; &nbsp; Derived<int> a;}

慕斯王

在C ++ 14中,您可以删除typedef和使用函数auto返回类型推导:template <typename derived_t>class base {public:&nbsp; &nbsp; auto foo() {&nbsp; &nbsp; &nbsp; &nbsp; return static_cast<derived_t*>(this)->foo();&nbsp; &nbsp; }};之所以可行,是因为将归还类型的推导base::foo延迟到derived_t完成为止。
打开App,查看更多内容
随时随地看视频慕课网APP