Given the following code:
void f() { class A { template <typename T> void g() {} }; }
g++ 4.4 (and also g++-4.6 -std=gnu++0x
) complains: "invalid declaration of member template in local class".
Apparently local classes are not allowed to have template members. What is the purpose of this limitation? Will it be removed in C++0x?
Note: If I make the local class itself a template, rather than giving it a template member:
void f() { template <typename T> class A { void g() {} }; }
I get "error: a template declaration cannot appear at block scope".
Member function templates are template functions that are members of a class or class template. Member functions can be function templates in several contexts. All functions of class templates are generic but are not referred to as member templates or member function templates.
No, template member functions cannot be virtual.
A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.
In C++ this can be achieved using template parameters. 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.
The purpose of this limitation? Just a guess, but:
Fun Fact: Try to use a local class within a function as a return type for a (c++0x)-lambda function declared in the function: MSVC 2010: internal compiler error ^^.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With