Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when using the template in C++

Recently, I meet something in other's source code. I don't quite understand the template in C++. Could you help me it?

struct my_grammar : public grammar<my_grammar>
{
    ...
};

Why my_grammar can be used as a type parameter like it?

Best Regards,

like image 232
Yongwei Xing Avatar asked Dec 22 '22 21:12

Yongwei Xing


1 Answers

This is an idiom called the Curiously Recurring Template Pattern - see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern. Summarily, it provides the derived class as a policy to the base class, in similar style to Policy's used in Alexandrescu's Modern C++ Design book (highly recommended). That way, the base class can use aspects of the derived class - types, constants, methods - all resolved at compile time.

like image 169
Tony Delroy Avatar answered Dec 24 '22 12:12

Tony Delroy