The return type of a generic function often depends on the types of the arguments. In some cases the dependency can
be expressed within the current language. For example:
1 2
| template<class T>
T min(const T& a, const T& b) { return a < b ? a : b; } |
In other cases this is not as easy, or even possible. Consider the following example:
1 2
| template<class A, class B>
R add(const A& a, const B& b) { return a + b; }} |
The return type, denoted here with R, should be the type of the expression a + b. That type may, however, be A, B,
or something entirely different. In short, the type R depends on the types A and B in a way that is not expressible in
today’s C++.
Partager