新的“使用”解决了什么问题,模板typedefs的语法? [英] What was the issue solved by the new "using" syntax for template typedefs?
问题描述
在C ++ 11中,您可以通过执行
In C++11 you can create a "type alias" by doing something like
template <typename T>
using stringpair = std::pair<std::string, T>;
但这是一个偏离你期望模板typedef看起来像:
But this is a deviation from what you'd expect a template typedef would look like:
template <typename T>
typedef std::pair<std::string, T> stringpair;
这就引出了一个问题 - 他们为什么需要提出一个新的语法?它是不是用旧的 typedef
语法工作?
So this raises the question - why did they need to come up with a new syntax? what was it that did not work with the old typedef
syntax?
我意识到最后一位不编译但是为什么不能编译呢?
I realize the last bit doesn't compile but why can't it be made to compile?
推荐答案
从WG21提案 N1489模板别名 (由Stroustrup和Dos Reis发布):
From the WG21 proposal N1489 Template aliases (by Stroustrup and Dos Reis):
已建议(重新)使用关键字
typedef
b paper [4]引入模板别名:
It has been suggested to (re)use the keyword
typedef
as done in the paper [4] to introduce template aliases:
template<class T>
typedef std::vector<T, MyAllocator<T> > Vec;
该符号的优点是使用已知的
引入一个类型别名。然而,它还显示几个不好的
,其中混淆使用已知的引入
别名的关键字在上下文中的别名不指定
a类型的类型名称,但是模板; Vec不是类型的别名,不应该为typedef名称采用
。名称Vec是系列的名称
std :: vector< o,MyAllocator< o> >
其中项目符号是类型名称的占位符
。因此,我们不建议使用typedef语法。
That notation has the advantage of using a keyword already known to
introduce a type alias. However, it also displays several disavantages
among which the confusion of using a keyword known to introduce an
alias for a type-name in a context where the alias does not designate
a type, but a template; Vec is not an alias for a type, and should not
be taken for a typedef-name. The name Vec is a name for the family
std::vector<o, MyAllocator<o> >
where the bullet is a placeholder
for a type-name. Consequently we do not propose the typedef syntax.
另一方面,
template<class T>
using Vec = std::vector<T, MyAllocator<T> >;
可以读取/解释为:从现在开始,我将使用 Vec< T>
作为
同义词 std :: vector< T,MyAllocator< T& >
。有了这个阅读,
新语法的混叠看起来是合理的。
can be read/interpreted as: from now on, I'll be using Vec<T>
as a
synonym for std::vector<T, MyAllocator<T> >
. With that reading, the
new syntax for aliasing seems reasonably logical.
上面提到的论文[4]报价是以前的提案WG21 N1406建议添加到C ++:Typedef模板 (由Herb Sutter提供)。它使用不同的语法( typedef
vs 使用
)以及不同的命名法(typedef模板vs模板别名) 。 Herb提出的语法没有实现,但有时可以在非正式讨论中找到命名。
The paper [4] referred to in the above quote was a prior proposal WG21 N1406 Proposed Addition to C++: Typedef Templates (by Herb Sutter). It uses both a different syntax (typedef
vs using
) as well as a different nomenclature (typedef templates vs template aliases). Herb's proposed syntax didn't make it, but the nomenclature can sometimes be found in informal discussions.
这篇关于新的“使用”解决了什么问题,模板typedefs的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!