Why can't templates be declared in a function?

后端 未结 7 713
甜味超标
甜味超标 2020-12-01 12:28

Reading C++ Templates: The Complete Guide and it says

Note that templates cannot be declared in a function

It does not give e

7条回答
  •  难免孤独
    2020-12-01 12:59

    The problem is probably linked to the historical way templates were implemented: early implementation techniques (and some still used today) require all symbols in a template to have external linkage. (Instantiation is done by generating the equivalent code in a separate file.) And names defined inside a function never have linkage, and cannot be referred to outside of the scope in which they were defined.

提交回复
热议问题