c++11 - Why do C++ templates use the angle bracket syntax? -


the titular question refers design decisions in c++ standard introduced templates around 1990.

why did designers use <> (angle brackets) instead of, say, () (round brackets)? doing have saved lots of programmers bit-shift related error

std::vector<std::vector<int>> // not work until c++11 

that got fixed in c++11. not see rationale of introducing additional syntax when, arguably, round brackets have served same purpose while keeping changes minimalist. insted have used

template(typename t) // define template if round brackets used mytemplate { ... } ... ... mytemplate(mytemplate(int)) obj; //instantiate template when round brackets used 

can well-versed in history of c++ dig out original design rationale using angle brackets? alternatively, can show why other solutions not have worked well?

templates introduced in 1988 usenix paper parameterized types c++ bjarne stroustrup, later incorporated the annotated c++ reference manual published in 1990 (the version before standardized c++). according paper,

the <…> brackets used in preference parentheses (…) partly emphasize different nature of template arguments (they evaluated @ compile time) , partly because parentheses hopelessly overused in c++.

9.2. <…> vs (…)

but why use brackets instead of parentheses? mentioned before, parentheses have many uses in c++. syntactic clue (the <…> brackets) can usedful reminding user different nature of type parameters (they evaluated @ compile time). furthermore, use of parentheses lead pretty obscure code:

template(int sz = 20) class buffer {     buffer(int = 10);     // ... }; buffer b1(100)(200); buffer b2(100);      // b2(100)(10) or b2(20)(100)? buffer b3;           // legal? 

these problems become serious practical concern if notation explicit disambiguation of overloaded function calls adopted. chosen alternative seems cleaner:

template<int sz = 20> class buffer {     buffer(sz)(int = 10);     // ... }; buffer b1<100>(200); buffer b2<100>;      // b2<100>(10) buffer b3;           // b3<20>(10) buffer b4(100);      // b4<20>(100) 

the paper explained why template , class keywords used.

note stroustrup placed <…> after variable name in same manner int x[10] argue against (…), though placement never used elsewhere in paper.

his argument "using (…) can lead obscure/ambiguous code" still valid though. mentioned in question's comment, using parenthesis t(x) leads ambiguity function type or function call (note t can function template , c++ allowed values template arguments).

similarly, using square brackets t[x] leads ambiguity array type or indexing.

i don't see why t{x} cannot used yet, maybe not considered, or maybe ugly have {…} used everywhere.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -