0

I am relatively new to C++(14) . And i am working on enhancing my current knowledge by reading through the official documentation from cpprefernce . I have some questions about template declaration : This page gives the following possible parameters(non-type template parameter) in the declarations for a class template .

type name(optional) // (1)

My question is :
1.)What is meant by type name ? I am sure it is not same as typename mentioned here.

Further on the same page , i see the example :

// simple non-type template parameter
template<int N>
struct S { int a[N]; };

However from //1 , i see the type name is optional , so i hypothesis the below should work but it does not:(Indeed i see the below declaration template<N> can be seen as template argument with type param.) \

// simple non-type template parameter
template<N>
struct S { int a[N]; };

So where is my understanding/reading of official docs is going wrong ?

Edit :

I tried the following :

template< int>
struct S { int a[N]; };

which does not compile with the error 'N' was not declared in the scope.

warrior_monk
  • 383
  • 2
  • 14
  • 3
    The _name_ is optional (the `N`), the _type_ is mandatory (the `int`). But in your code you actually need the name, so you can't omit it. – Barry Jan 21 '20 at 04:05
  • Can you give an example , where the name is not given but just the type ? I tried making such an example in the edit , but does not work either . – warrior_monk Jan 21 '20 at 04:14
  • 1
    Well, sure. It doesn't work because you actually need the name. This works: `template struct S { };` – Barry Jan 21 '20 at 04:22
  • Now i see what you mean . But i see this declaration , won't solve any purpose . So the official docs just mean , that the declaration is possible ,irrespective of whether it is useful or not ? – warrior_monk Jan 21 '20 at 04:48
  • 1
    It's useful for forward declarations. `template struct S;` – Indiana Kernick Jan 21 '20 at 05:04
  • 1
    Just an aside - cppreference is not an *official* set of docs. They're very useful though. – Marshall Clow Jan 21 '20 at 05:55
  • You might be interested by [whats-the-point-of-unnamed-non-type-template-parameters](https://stackoverflow.com/questions/59824884/whats-the-point-of-unnamed-non-type-template-parameters) – Jarod42 Jan 21 '20 at 09:19

1 Answers1

1

For that to work, N would need to be a type.

Templates only accept a type followed by an optional name for that type. So for instance this might work (though I haven't tried it):

template <int> // ...

But note that N is not declared. If you want to use N in your template, you will need to declare N like this:

template <int N> // ...

Here, N is declared as an int, so you can use it just as you would an int.


Note: You also have the option of something like this:

template <class N> // ...

Here N becomes a template replacement for a type. It isn't an int as above.

Of course, that's probably not what you want.

  • I see the `template struct S { }` works from Barry's comment . But then i think it does not solve any purpose . From @Kerndog73's answer , i see this can be used in forward declaration of template class . I have never used a forward declaration for template classes though . Is there any other use of such declaration ? – warrior_monk Jan 21 '20 at 23:24
  • A forward declaration of a template class? I would assume the same reasons you would use a forward declaration in the first place would still apply. –  Jan 22 '20 at 00:10