C ++ Element Template Syntax Syntax

In this site contains the following paragraph:

When defining a member of an explicitly specialized class template outside the class body, the <> syntax template is not used, unless it is a member of an explicitly specialized member class template that specializes as a class template , because otherwise the syntax will require such a definition to start with template <parameters> required for a nested template .

I do not know what the highlighted section means. “Otherwise” refers to the general case (in which the template <> is not used) or to the case of the exception (in which the template <> should be used)?

I would appreciate an explanation of this section.

+4
source share
1 answer

This will be our template:

template< typename T>
struct A {
    struct B {};  // member class 
    template<class U> struct C { }; // member class template
};

See the following code:

template<> // specialization
struct A<int> {
    void f(int); // member function of a specialization
};
// template<> not used for a member of a specialization
void A<int>::f(int) { /* ... */ }

We do not use template<>when defining a member of a specialization, since this is a normal member class.

But now let's see the following code:

template<> // specialization of a member class template
template<class U> struct A<char>::C {
    void f();
};

// template<> is used when defining a member of an explicitly
// specialized member class template specialized as a class template
template<>
template<class U> void A<char>::C<U>::f() { /* ... */ }

Here we specialize in a member template for a given template. That's why we need to use template<>to pass the parameters that are required for this member template. In this case, class Uit is necessary to determine our member template, so for the transfer we need a keyword template<>.

-1
source

Source: https://habr.com/ru/post/1696097/


All Articles