As someone who is extremely unfamiliar with category theory, types, and C++, something is bothering me that I would love to have clarified.
Polymorphic function templates are introduced with addEm:
template <typename T, typename U>
T addEm(T a, U b)
{
return a + b;
}
It seems to me that this is incorrect. From the Wikipedia page on C++ templates [1], a polymorphic function template is described like this:
template <typename T>
inline T max(T a, T b) {
return a > b ? a : b;
}
The first example seems to define a template so that the function takes two arguments of different types, then relies on the compiler to reject inputs for `+' of types not defined for it. (This does not seem to track with the explanation following it, which indicates that addEm should take two arguments of the same type.)
The implication in the first example seems to be that if you want your template to be compatible with more than one type, you must explicitly indicate so in your template parameters. However, the Wikipedia entry seems to indicate that C++ implicitly instantiates object code for as many types as the template is called on at compile time, a function instantiated for each of the types (addEm<string>, addEm<int>, addEm<double>, etc.), and a compilation error resulting if inputs of different types are passed to the template.
Am I missing something? For reference, here is how I would intuit the first example should look, based on my current understanding.
template <typename T>
T addEm(T a, T b)
{
return a + b;
}
I'll add that given my lack of knowledge of C++, it could well be that the template syntax allows types T and U to be the same in a particular instantiation of the template, but my understanding is that at least they can be different, and why would you intentionally do that in this example?
Polymorphic function templates are introduced with addEm:
It seems to me that this is incorrect. From the Wikipedia page on C++ templates [1], a polymorphic function template is described like this: The first example seems to define a template so that the function takes two arguments of different types, then relies on the compiler to reject inputs for `+' of types not defined for it. (This does not seem to track with the explanation following it, which indicates that addEm should take two arguments of the same type.)The implication in the first example seems to be that if you want your template to be compatible with more than one type, you must explicitly indicate so in your template parameters. However, the Wikipedia entry seems to indicate that C++ implicitly instantiates object code for as many types as the template is called on at compile time, a function instantiated for each of the types (addEm<string>, addEm<int>, addEm<double>, etc.), and a compilation error resulting if inputs of different types are passed to the template.
Am I missing something? For reference, here is how I would intuit the first example should look, based on my current understanding.
I'll add that given my lack of knowledge of C++, it could well be that the template syntax allows types T and U to be the same in a particular instantiation of the template, but my understanding is that at least they can be different, and why would you intentionally do that in this example?1. https://en.wikipedia.org/wiki/Template_(C%2B%2B)