5

Is there any way to display template method in UML (I mean a c++ template method, not pattern)? I've found template classes only.

Suppose i have

class A {
  public:
    template <typename T>
    std::vector<T> func(T& var);
};

So, the only way I can display it in uml now is +func(in var : T&) : std::vector<T>. Is there any way to say it's template, and T is not a usual type?

Roman
  • 1,396
  • 4
  • 15
  • 39
  • How about google? http://www.google.de/imgres?imgurl=https://www.usenix.org/legacy/publications/library/proceedings/coots01/full_papers/duret/duret_html/dpgen-duret002.png&imgrefurl=https://www.usenix.org/legacyurl/coots-2001-paper-4&h=480&w=508&tbnid=UNpMyupff1yIEM:&zoom=1&tbnh=90&tbnw=95&usg=__QJNRmAAPdR6SeJVaQq62iZsir2U=&docid=o3ch2dCQms3J8M&client=safari – qwerty_so Apr 07 '15 at 13:52
  • 1
    Somewhat limiting that UML only recognised functions as parts of classes - as we have constructs which *require* them to be namespace level items. Meanwhile model them as function objects? There is also another limitation - how to represent template methods of a class, where the methods template parameters are not part of the class...? – simon.watts Feb 14 '17 at 15:41
  • Does this answer your question? [How to represent generic parameter in UML method?](https://stackoverflow.com/questions/16526051/how-to-represent-generic-parameter-in-uml-method) –  Aug 24 '21 at 14:49

3 Answers3

6

I'm not entirely sure here, but I think the answer is no.

If I understand it correctly, UML can only deal with templates on a class level -- meaning that your class A would need to be specified as a template class in order for func() to be valid.

Put another way, UML does not allow template parameters to be introduced in an operation without also being specified in the class signature. I think.

Bear in mind always that UML is not a programming language and in fact tends to break down when confronted with non-trivial source code constructs in any particular language.

More information here.

Community
  • 1
  • 1
Uffe
  • 10,396
  • 1
  • 33
  • 40
3

I've been forced to represent them as individual templated function objects. This is only for the purpose of representation and modelling though.

So

template<typename T> void foo (T& t) { ... }

is modelled as:

template<typename T> class foo
{
    void operator() (T& t) { ... }
}

Its a hack.

simon.watts
  • 976
  • 10
  • 14
0

I think this link shows what you are looking for.

Template on UML diagram

user1880062
  • 85
  • 1
  • 5
  • 4
    Thanks anyway, but it's not what I'm looking for. Article tells about representing template classes, not methods. – Roman Apr 07 '15 at 13:45