【问题标题】:C++ Base Class using Derived Class Template [duplicate]使用派生类模板的 C++ 基类 [重复]
【发布时间】:2018-04-22 23:07:02
【问题描述】:

我正在尝试从 dlib 项目中了解以下类型的类。

template<typename EXP>
class matrix_exp
{
public:
    //typedef matrix<type, NR, NC, mem_manager_type, layout_type> matrix_type;
    matrix_exp()
    {
        cout << "matrix_exp()" << endl;
        cout << __PRETTY_FUNCTION__ << endl;
    }
    void foo()
    {
        cout << "matrix_exp" << endl;
    }
};

template<typename T, long num_rows, long num_cols, typename mem_manager, typename layout>
class matrix: public matrix_exp<matrix<T, num_rows, num_cols, mem_manager, layout> >
{
public:

    void foo()
    {
        cout << "matrix" << endl;
    }
};

基类如何使用派生类作为模板?他们还使用了 typedef 矩阵 matrix_type;在 matrix_exp 中。有人可以澄清这是如何工作的吗?

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    这就是众所周知的Curiously Recurring Template Pattern (CRTP)

    有一个 StackOverflow tag 仅用于 CRTP。

    history of CRTP is on Wikipedia

    【讨论】:

    • 这么明显的重复不用回答了;只需标记它。
    • Thnaks,这个词对我来说是新的。很高兴知道这个词。
    猜你喜欢
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2013-12-17
    相关资源
    最近更新 更多