0

I am trying to understand the following kind of classes from the dlib project.

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

How is the based class uses derived class as it's template? They have also used typedef matrix matrix_type; inside the matrix_exp. Can someone clarify how this works?

max
  • 1,692
  • 5
  • 28
  • 40

1 Answers1

1

That's known as the Curiously Recurring Template Pattern (CRTP).

There is a StackOverflow tag just for CRTP.

The history of CRTP is on Wikipedia.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
OznOg
  • 4,440
  • 2
  • 26
  • 35