I have the following nested template
class A {
template <typename T> class B {
template <typename U> void foo(U arg);
};
};
I am trying to define the nested template like so:
template <typename T, typename U> void
A::B<T>::foo(U arg) {...}
But am getting declaration is incompatible with function template
error. What's the legal syntax to do so?
Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. To simply put, you can create a single function or single class to work with different data types using templates. C++ template is also known as generic functions or classes which is a very powerful feature in C++.
" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.
3. Which of the following best defines the syntax for template function ? Explanation: Both A or B is the syntax for template function. Explanation: Templates are abstract recipe for producing a concrete code, and it is used for Both A and B options.
You need to separate the template declarations:
template <typename T>
template <typename U>
void
A::B<T>::foo(U arg) { … }
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