Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm thinking about curiously recurring template pattern.

I find it quite useful, and it's pretty well adjustable for many tasks. But I have to admit, I don't see much of CRTP code in the wild.

Is it hard to grasp? Are virtual functions easier overall? Shouldn't it be used for 90% of cases where virtual keyword appears?

share|improve this question

closed as primarily opinion-based by MichaelT, gnat, GlenH7, Dynamic, BЈовић Jul 20 '13 at 21:07

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 8 down vote accepted

The simple fact is that many, if not most, C++ users can hardly grasp that vector is better than malloc and free, so the number who can not only appreciate but implement advanced template metaprogramming is not very high. This implies that even if it were good for 90% of situations, you'd see it in far less, simply because people don't know about it or how to use it.

However, virtual functions do have advantages, namely, virtual functions are much easier to use than the CRTP. C++ templates are notorious for being bitchy about virtually anything and everything, and the inclusion model sucks balls.

However, they [virtual functions] are distinctly slower and less powerful. For example, I right now am writing code using the CRTP, where you inherit operator overloads for use with expression templates. There's no way virtual functions could possibly make that work.

share|improve this answer

WTL uses the CRTP. Chromium browser is a well known example of software that uses WTL, so I'd say it's used "in the wild".

About the 90%? That's definitely an overstatement. Have you ever used a container of polymorphic objects? That's a very common usage of polymorphism and you can't do that with CRTP because Base<DerivedA> is not the same as Base<DerivedB>.

share|improve this answer

Here's reasons why it's not used more widely:

  1. It's a hack
  2. As a hack, it's not guaranteed you don't run into trouble when you use it. The normal OO stuff has been used by large number of projects, and found to work properly/problems are well known, the CRTP is found in only small number of projects, and no data is available
  3. there is no good reason to use it - most reasons sited are just not valid
  4. it can make some things more complicated
share|improve this answer
It is most assuredly not a hack. The behaviour is very well defined, and there's plenty of use of it. – DeadMG Dec 8 '11 at 20:41
Yep, whole STL is a big hack. – Abyx Dec 8 '11 at 20:58
can you edit your answer and explain, why it is a hack? – Nick Sep 28 '15 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.