0

This is not a question about template template syntax meaning, but rather a question about why this syntax was chosen by C++ standard committee. Lets take this example,

(1)

template< typename T> 
Class A
{
...
}

The above one is syntax of a class template syntax with single template argument. I can understand this syntax is something c++ committee can choose arbitrarily.

(2)

template<template <typename> typename T>
class B
{
...
} 

C++ standard committee should have built (2) syntax using (1) with some language standard/ grammar. I could not understand how (2) was built using (1).

If given a choice I would have chosen the below as template template syntax,

(3)

template <typename template< typename T>> //my way of writing syntax (but not recognized by standard)

The inner template<typename T> in above is a template datatype, so a template of that should be written template<typename template<typename T>>

Can anyone explain why the language committee chose (2) instead of (3)

There is a similar question Syntax of C++ Template Template Parameters which doesn't clarify these

Karthick S
  • 311
  • 5
  • 13

1 Answers1

1

It does make sense if you break it down step by step. If we start with a declaration of a templated structure T:

template <typename U>
struct T;

If we have a class B which is a template:

template <typename T>
struct B;

If we replace typename T with our declaration above:

template <template <typename U> struct T>
struct B;

Replace struct with typename:

template <template <typename U> typename T>
struct B;

Then we just need to remove the U (though you don't have to, you can leave it in, it just has no meaning) and we get:

template <template <typename> typename T>
struct B;

Your syntax of:

template <typename template<typename T>>

Would imply that T is the type of the parameter of the inner template (U in my example) not the outer template type, this would make more sense which isn't too far from the standard:

template <typename template<typename> T>
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • I wouldn't have replaced `T` but `typename T`. That way you don't have a redundant typename to remove which in turn begets the question which of the two to remove. – Ulrich Eckhardt May 17 '20 at 07:21
  • @UlrichEckhardt yeah I was thinking that too – Alan Birtles May 17 '20 at 07:23
  • @AlanBirtles : I like your answer. It answers my question and explains with details. I am confident this will be useful for others. Thanks for your time! – Karthick S May 17 '20 at 09:51