Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is template<> template<> syntax in the context of template-alias for?

Tags:

c++

templates

(This question is not about template template arguments.)

I just discovered that GCC compiles such code

template <typename A, typename B>
struct P {};

template <typename A>
template <typename B>
using Q = P<A, B>;

where Q is a doubly-templated name.

But I can't use this. When I write Q<short><long>, I get

template_template.cpp:10:5: error: ‘Q<short int>’ is not a template
     Q<short><long>{};
     ^~~~~~~~
template_template.cpp:10:20: error: invalid use of incomplete type ‘Q<short int>’
     Q<short><long>{};
                    ^
template_template.cpp:2:8: note: declaration of ‘Q<short int>’
 struct P {};

Why is the first snippet compiled?

Is there a syntax to convince the compiler that Q<short> is actually a template?

// GCC 6.3.0

like image 885
Yuri Syro Avatar asked Oct 20 '17 12:10

Yuri Syro


People also ask

What does template <> mean in CPP?

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++.

What is template type alias?

Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.

What is a template template parameter in C++?

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.

What is an alias test C++?

In C++11, a type alias is a name for another already declared type, and an alias template is a name for another already declared template. Both of these types of aliases are introduced with a new using syntax.


1 Answers

The C++14 standard says in 14p1:

The declaration in a template-declaration shall
— declare or define a function, a class, or a variable, or
— define a member function, a member class, a member enumeration, or a static data member of a class template or of a class nested within a class template, or
— define a member template of a class or class template, or
— be an alias-declaration

Here the declaration within the template-declaration is none of the above (it is another template-declaration, which itself contains an alias-declaration), and therefore the code is invalid.

The relevant parts of the grammar are:

template-declaration:
    template <template-parameter-list> declaration

alias-declaration:
    using identifier attribute-specifier-seqopt = type-id ;

where a declaration can be a template-declaration, an alias-declaration, or other types of declarations.

Note that the grammar itself accepts the given code, but the additional restrictions in the text above make it invalid.

like image 52
interjay Avatar answered Sep 17 '22 21:09

interjay