Yongwei Xing
Yongwei Xing

Reputation: 13451

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,

Upvotes: 4

Views: 145

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106236

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.

Upvotes: 11

Related Questions