May 2021 1 111 Report
Problem understanding CRTP [C++]?

Here is the code : https://ideone.com/plt3Y

template <typename Derived>

class CuriousBase {

};

template <typename T>

class CuriousTemplate : public CuriousBase<CuriousTemplate<T> > {

};

My Question :

**class CuriousTemplate : public CuriousBase<CuriousTemplate<T> > {};** , here is what my confusion lies.

To make object of "CuriousTemplate<T>" , it has to build it's sub-object "CuriousBase<CuriousTemplate<T>>".

To build "CuriousBase<CuriousTemplate<T>>", It has to instantiate "CuriousTemplate<T>" but to instantiate "CuriousTemplate<T>", Again it has build base object "CuriousBase<CuriousTemplate<T>>" and so on ...

Isn't this is infinite inheritance? How this all is working?

Important Q: Am I forgetting some special rule of template instantiation here?

Does this has anything to do with empty base class optimization? , If yes please explain me this too.

Thanks a lot , I really appreciate the help :)


Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments




Helpful Social

Copyright © 2024 Q2A.MX - All rights reserved.