Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't templates be declared in a function?

Tags:

c++

templates

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

Note that templates cannot be declared in a function

It does not give explanation and/or cross reference to any other chapter in the book or external resource.

Could someone help in explaining this. Probably it is explained later in the book but not there yet. If explained earlier, I must have missed it.

Example:

int main() {   class DummyClass  //  This compiles ok   {     int object;   };    template <typename T> //  compile error "expected primary-expression before "template""   class DummyTemplate   {     T object;   };    return 0; } 

I do not understand the error message from gcc either. The error message says:

expected primary-expression before "template" 
like image 993
dubnde Avatar asked Aug 10 '10 12:08

dubnde


People also ask

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.

Can we use template in main function?

main cannot be a function template; it must be a function.

What Cannot be declared as template in C++?

Q. Which of the following cannot be declared as template ? Correct Answer : OPTION D, Macros. Macros are implemented in a preprocessor and cannot be implemented as a template.

Can a template parameter be a function?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.


2 Answers

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.

like image 51
James Kanze Avatar answered Sep 21 '22 11:09

James Kanze


The answer "because standard says so", is of course correct, but let's consider generic lambdas.

In C++14 and C++17 generic lambdas are the only way of writing template-like code that I know of:

    auto lambda = [](auto x) { };     lambda.operator()<int>(0); 

Technically, you can write any kind of template code just with that. Though you'll have to work hard to work around various limitations of this approach.

That will be simpler in C++20 though. With template parameter list in generic lambdas you will be able to write code like this:

    auto size = []<class T>() { return sizeof(T); };     static_assert(4 == size.operator()<int>()); 

GCC already supports this syntax.

like image 42
D. Dmitriy Avatar answered Sep 22 '22 11:09

D. Dmitriy