Templates were introduced in the 1988 USENIX paper Parameterized Types for C++ by Bjarne Stroustrup, later incorporated into The Annotated C++ Reference Manual published in 1990 (the version before standardized C++). According to the paper,
The <…>
brackets are used in preference to the parentheses (…)
partly to emphasize the different nature of template arguments (they will be evaluated at compile time) and partly because parentheses are already hopelessly overused in C++.
…
9.2. <…>
vs (…)
But why use brackets instead of parentheses? As mentioned before, parentheses already have many uses in C++. A syntactic clue (the <…>
brackets) can be usedful for reminding the user about the different nature of the type parameters (they are evaluated at compile time). Furthermore, the use of parentheses could lead to pretty obscure code:
template(int sz = 20) class buffer {
buffer(int i = 10);
// ...
};
buffer b1(100)(200);
buffer b2(100); // b2(100)(10) or b2(20)(100)?
buffer b3; // legal?
These problems would become a serious practical concern if the notation for explicit disambiguation of overloaded function calls were adopted. The chosen alternative seems much cleaner:
template<int sz = 20> class buffer {
buffer(sz)(int i = 10);
// ...
};
buffer b1<100>(200);
buffer b2<100>; // b2<100>(10)
buffer b3; // b3<20>(10)
buffer b4(100); // b4<20>(100)
The paper also explained why the template
and class
keywords are used.
Note that the Stroustrup placed the <…>
after the variable name in the same manner as int x[10]
to argue against (…)
, though this placement is never used elsewhere in the paper.
His argument that "using (…)
can lead to obscure/ambiguous code" is still valid though. As mentioned in this question's comment, using parenthesis T(x)
leads to ambiguity with function type or function call (note that T
can be a function template and C++ allowed values to be template arguments).
Similarly, using square brackets T[x]
leads to ambiguity with array type or indexing.
I don't see why T{x}
cannot be used yet, maybe it was simply not considered, or maybe it is too ugly to have {…}
used everywhere.
int(int(int)) obj;
would makeobj
a pointer to a function which returnsint
and takes a pointer to a function which returnsint
and takesint
. Or something like that. – Tableauint obj(int(int))
. – Featheringtemplate_function(int())()
, isint()
a template argument or a function argument? – Velar<>
? That almost certainly is not the process that was used to decide on<>
: describing the entire process is too broad for SO. Anything else is an opinion, simplification, or lie. The short answer is "that is what was standardized, which is why we use it". I doubt that would satisfy. Does the OP want minutes? Recollections of people on the committee at the time? – Way<>
and normal function parameters()
should be changed to use curly braces{}
. ThenC++
be like:{class A}class B{B{}{}}; B{int} b{};
So much easier... – Divulsion<.*?>
in plaintext is automatically stripped, unless it's one of a few allowed HTML tags. In this case,<angle brackets>
met those critera, so it was removed from the rendered output. I've fixed the issue in this one, but it's worth keeping in mind in the future. Also, if you don't like my fix, please feel free to change it, but keep in mind the requirements for them to be visible. – RefaceTemplatedType!(T)
, which doesn't have the ambiguity problem because there's not binary!
in C++. – Grose`
,#
,@
. I don't know why they're not considered – Mannie