3

As per http://en.cppreference.com/w/cpp/language/type_alias, aliases are block-level declarations. It doesn't say anything special about template aliases, so it should be read that template aliases are block-level declarations as well.

However, it is impossible to use template aliases at block level. The errors are different depending on the compiler - while g++ gives a meaningful message, saying that templates are not allowed at block scope, clang is completely cryptic. (example: http://coliru.stacked-crooked.com/a/0f0862dad6f3da61).

Questions I have so far:

  • Does cppreference fail to specify that template aliases can not be used at block scope? (Or do I need to take a reading course?)
  • Are the compilers correct in denying template aliases on block level (the feature I find very interesting for my particular coding habits)
  • If the answer to the second is Yes, what might be the rationale for this? Why would compiler deny me this pure syntax sugar?
SergeyA
  • 61,605
  • 5
  • 78
  • 137

2 Answers2

6

An alias template is [temp.alias]

A template-declaration in which the declaration is an alias-declaration (Clause 7) declares the identifier to be a alias template. An alias template is a name for a family of types. The name of the alias template is a template-name.

And if we look at 14.2 [temp] we have

A template-declaration can appear only as a namespace scope or class scope declaration. In a function template declaration, the last component of the declarator-id shall not be a template-id.

So yes cppreference is off saying that it can be declared at block scope and your compilers are correct. If you do click on the link of block declarations It will bring you to a list of declarations and in that it has Template declaration and in there it has

declaration of a class (including struct and union), a member class or member enumeration type, a function or member function, a static data member at namespace scope, a variable or static data member at class scope, (since C++14) or an alias template (since C++11) It may also define a template specialization.

As for why the standard says that templates can only be declared in namespace scope or class scope I like James Kanze answer

The problem is probably linked to the historical way templates were implemented: early implementation techniques (and some still used today) require all symbols in a template to have external linkage. (Instantiation is done by generating the equivalent code in a separate file.) And names defined inside a function never have linkage, and cannot be referred to outside of the scope in which they were defined.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Alright, compilers are behaving per standard. Now, do you know the answer to my last question? – SergeyA Sep 24 '15 at 18:20
  • @SergeyA What do you mean? your last question is moot as we cannot declare templates at block scope. – NathanOliver Sep 24 '15 at 18:22
  • Well, this is (even if branded so by standard) not a template declaration. It is template alias declaration, and there is a difference between those two. Again, I see templare alias a pure syntax sugar. What are the reasons to forbid it's declaration at block scope? What would it break if allowed? – SergeyA Sep 24 '15 at 19:10
  • @SergeyA No there is not a difference. You still have to specify the type with a alias template. Take `template using vec2d = std::vector>;` we would still have to specify the type when we use `vec2d` like `vec2d foo;` If you are using the template keyword then it is a template declaration. – NathanOliver Sep 24 '15 at 19:26
  • This is fine. The question is, why is it prohibited. There should be a reason, like compilers can't cope with it, it would be unsafe, etc. Some reason. I do not see it. template using ve2d = std::vector > is just a shorthand. So whenever compiler sees ve2d it can exapand it into std::vector > . I do not see why needs to be prohibited. And this is the question I am asking. – SergeyA Sep 24 '15 at 19:43
  • Sorry, can't accept it :) I understand why templates themselves can not be defined inside functions. But template aliases do not have to be treated this way. If I take James' answer and try to apply it to template alias scenario, I do not see how it breaks anything. Template does provide a template for code generation, and thus needs symbols. True. But alias does not! The template which is referred by this alias - yes. But not the alias itself! – SergeyA Sep 24 '15 at 20:04
  • @cubbi awesome. Thank you. I did not know people could edit/contribute to it. – NathanOliver Sep 25 '15 at 23:48
  • @Cubbi, Hi :) Do you know the answer to my question by any chance? (and I hope you've recognized your former collegue in my face ;) – SergeyA Oct 29 '15 at 21:11
1

The compilers are behaving correctly.

Section 14 of the C++14 standard:

A template-declaration can appear only as a namespace scope or class scope declaration.

KyleKnoepfel
  • 1,426
  • 8
  • 24