2

How do I correctly draw an UML diagram for the C++ Curiously recurring template pattern (CRTP) without replicating the base class for every inherited class, i.e. in a way that reflects the implementation rather than the situation after compilation?

The question boils down to:what do I write into the dashed box of the common template-baseclass if the template parameter can be any of a whole set of derived classes?

selbie
  • 100,020
  • 15
  • 103
  • 173
user2961818
  • 721
  • 5
  • 11
  • that doesn't answer my question; the case that the template parameter must be a derived class isn't mentioned at all. – user2961818 Feb 10 '20 at 09:28
  • I re-opened that since the answer https://stackoverflow.com/questions/11453540/how-can-i-represent-inheritance-from-a-template-parameter-in-uml was from 2012 (no idea which U.M.L. - lol - version was out that time). – qwerty_so Feb 10 '20 at 10:24

1 Answers1

3

As said in the other answer an example of the C++ Curiously recurring template pattern (CRTP) is :

template <class T>
class Base
{
    // methods within Base can use template to access members of Derived
};
class Derived : public Base<Derived>
{
    // ...
};

what do I write into the dashed box of the common template-baseclass if the template parameter can be any of a whole set of derived classes?

The dashed box of the template class shows the template parameters, in case of the class Base :

enter image description here

The fact the template parameter can be any of a whole set of derived classes is not relevant because it can be anything else.

Adding the derived class in the diagram to have the full CRTP:

enter image description here


From your remark :

that doesn't answer my question; the case that the template parameter must be a derived class isn't mentioned at all.

In your question you just say it can be, now you want it must be.

Note CRTP is defined by the two classes, not only by the base class. Anyway if you want to have that limitation concerning the template parameter (T) just use a constraint, may be T.parents->includes(Base) even I am not sure of the use of T in it, it must apply on the value(s) of T rather than T itself which is a class

enter image description here


From your remark to my answer :

Another idea: would it be allowed to draw a collaboration connector from the base-classes T in the dashed box down to every depicted class that is derived from base?

I do not see how a collaboration can appear in that case, may be you think about a dependency ?

For me you cannot drawn relations from T in the dashed box, and even it is possible that does not indicate what you want.

Rather than to draw a relation from T to the derived classes just draw the derived classes with each time the inheritance and bind, that is the right way in UML.

enter image description here

bruno
  • 32,421
  • 7
  • 25
  • 37
  • my formulation "can be" may admittedly be interpreted in a way that doesn't exclude non-derived classes, but in the context of CRTP I think it is clear that only derived classes are to be considered as a template parameter; my apologiies for that.. – user2961818 Feb 11 '20 at 12:07
  • Another idea: would it be allowed to draw a collaboration connector from the base-classes T in the dashed box down to every depicted class that is derived from base? – user2961818 Feb 11 '20 at 12:10
  • @user2961818 so yes you want *can*, by definition without constraint all is possible including the fact you have the CRTP or not. I edited my answer to answer to your second remark – bruno Feb 11 '20 at 13:42
  • thank you very much for the updated diagram; that refects what I had hoped for.! – user2961818 Feb 11 '20 at 14:02