Why can't templates be declared in a function?

后端 未结 7 710
甜味超标
甜味超标 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:58

    It means you cannot do something like the following

      void foo()
      {
           template  //Error
           T something;
      }
    

    Template declarations are only permitted at global, namespace, or class scope. :)

    What is the reasoning behind it?

    It is not allowed because the Standard says so .

    ISO C++-98 (Section 14.2)

    A template declaration can appear only as a namespace or class scope declaration.

    Does that make sense?

提交回复
热议问题