模板别名作用域 [英] Template alias scope

查看:411
本文介绍了模板别名作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://en.cppreference.com/w/cpp/language/ type_alias ,别名是块级声明。它不会对模板别名说什么特别的,所以应该读取模板别名也是块级别的声明。



但是,不可能使用模板块级别的别名。错误根据编译器而不同 - 而g ++提供了一个有意义的消息,说模块在块范围不允许,clang是完全隐藏的。 (示例: http://coliru.stacked-crooked.com/a/0f0862dad6f3da61 ) 。



我到目前为止的问题:




  • cppreference无法指定该模板别名不能在块范围使用?

  • 编辑器在拒绝块级别的模板别名时是正确的(我发现我的特定编码习惯非常有趣)

  • 如果第二个答案是是,那么这可能是什么原因?为什么编译器会拒绝我这个纯的语法糖?


解决方案

别名模板是[temp.alias]


声明是别名声明的模板声明(第7条)将
的标识符声明为别名模板。别名模板是一系列类型的名称。别名模板的名称是
模板名。


如果我们看看14.2 [temp]


模板声明只能作为命名空间范围或类范围声明出现。在函数
模板声明中,declarator-id的最后一个组件不应该是模板id。


是cppreference关闭说它可以声明在块范围和您的编译器是正确的。如果您点击阻止声明的链接,它将带您到列表的声明,并且它具有模板声明,并且在其中具有



类(包括struct和union)的声明,成员类或成员枚举类型,函数或成员函数,命名空间中的静态数据成员范围,类范围的变量或静态数据成员(自C ++ 14起)或别名模板(自C ++ 11起)也可以定义模板特化。


至于为什么标准说模板只能在命名空间范围或类范围中声明我喜欢 James Kanze 回答


问题可能与模板实现的历史方式有关:早期实现技术(和今天仍然使用的一些)需要模板中的所有符号具有外部链接。 (通过在单独的文件中生成等效代码来实现实例化)。在函数中定义的名称不具有链接,并且不能在它们定义的范围之外引用。


< blockquote>

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?

解决方案

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.

这篇关于模板别名作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆