为什么C ++中的类型别名在其语法中使用“ using”而不是“ typedef”? [英] Why do type aliases in C++ use 'using' instead of 'typedef' in their syntax?

查看:162
本文介绍了为什么C ++中的类型别名在其语法中使用“ using”而不是“ typedef”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很明显,类型别名和模板化类型别名在语义上等效于typedef和typedef的扩展以支持模板。如何为这些关键字创建使用 using 关键字的新语法,而不是使用typedef作为第一个语法扩展名以及使用单词 typedef

Clearly, type aliases and templated type aliases are semantically equivalent to typedefs and an extension of typedefs to support template. How come new syntax with the using keyword was created for these instead of using typedefs for the first and some syntax extension with the word typedef.

注意:这不是使用和typedef之间的差异问题的副本。我知道使用的好处是可以定义一个 typedef s系列。我要问的是,为什么标准人决定让此扩展名使用 using 关键字而不是 typedef 关键字。

NOTE: This is not a clone of the "difference between using and typedef" question. I know that using gives the advantage of defining a family of typedefs. What I am asking is why did the standard people decide on having this extension use the using keyword instead of the typedef keyword. This seems like it just adds confusion in the language.

推荐答案

您的建议实际上是2002年在文档 N1406 。例如,它将允许编写:

What you suggest was actually proposed back in 2002 in document N1406 by Herb Sutter. It would allow, for example, to write:

template<typename T> typedef X<T,int> Xi; 

后来在N1449 (由Gabriel Dos Reis和Mat Marcus撰写)。他们采用使用语法,并注意以下内容:

This was later revised in N1449 by Gabriel Dos Reis and Mat Marcus. They adopt the using syntax, and note the following:


请注意,我们专门避免使用术语 typedef template,并引入涉及 using和 =对的新语法,以帮助避免混淆:我们此处未定义任何类型,我们为类型的抽象引入了同义词(即别名) -id(即类型表达式),涉及模板参数。

Note that we specifically avoid the term "typedef template" and introduce the new syntax involving the pair "using" and "=" to help avoid confusion: we are not defining any types here, we are introducing a synonym (i.e. alias) for an abstraction of a type-id (i.e. type expression) involving template parameters.

它们还指出:


进行了两次语法查询。绝大多数投票赞成避免使用typedef模板语法,而赞成使用 =语法。第二次投票表明,强烈倾向于使用关键字,而不是像本提案草案中的 alias之类的单词或缺少任何关键字。完全使用任何关键字的动机部分源于对使用可能与上面简要概述的非模板别名方向兼容的语法的渴望。

Two straw polls were taken regarding syntax. A strong majority voted to avoid the typedef template syntax, in favor of the "=" syntax. A second vote indicated strong preference for the "using" keyword as opposed to a word like "alias" or the absence of any keyword as in the draft version of this proposal. The motivation for using any keyword at all stemmed partly from the desire to use a syntax that might be compatible with the non-template aliasing direction briefly outlined above.

此语法随后在最终提案 N2258 加布里埃尔·多斯·雷斯(Gabriel Dos Reis)和比耶·斯特劳斯特鲁普(Bjarne Stroustrup)。

This syntax was then adopted in the final proposal N2258 by Gabriel Dos Reis and Bjarne Stroustrup.

这篇关于为什么C ++中的类型别名在其语法中使用“ using”而不是“ typedef”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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