为什么仍然需要在using语句的RHS中消除类型为typename的依存类型的歧义? [英] Why does one still need to disambiguate a dependent type with typename in the RHS of a using statement?

查看:220
本文介绍了为什么仍然需要在using语句的RHS中消除类型为typename的依存类型的歧义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚为什么需要对依赖类型使用typename,因为当编译器看到类似T::type之类的东西时,它可能无法消除类型和变量声明之间的歧义. 此答案以得到很好的解释. TL; DR :在类似T::type * x;的表达式中,编译器无法知道" T::type是类型还是在T的某些特定专业化中声明的变量.

I know very well why one needs to use typename for dependent types, since the compiler may not be able to disambiguate between a type and a variable declaration when it sees something like T::type, see e.g. this answer for a great explanation. TL;DR: in an expression like T::type * x;, the compiler cannot "know" whether T::type is a type or perhaps a variable declared in some particular specialization for T.

但是,类似

using type = T::type;

没有任何歧义. IMO,T::type 应该始终被解析为类型,因为它是using语句的RHS的一部分.但是,我们仍然需要在这里使用typename(至少根据gcc和clang而言),

there is nothing ambiguous. IMO, T::type should always be parsed as a type, as it's part of the RHS of a using statement. However, we still need to use the typename here (at least according to gcc and clang),

using type = typename T::type;

在Coliru上直播,gcc 在Co里的科利鲁生活

Live on Coliru, gcc Live on Coliru, clang

Visual C ++ 似乎在没有typename的情况下接受了代码,但是我没有太多的信心在编译器中完全符合标准(实际上,它具有许多非标准扩展名,例如将rvalue绑定到非const引用).

Visual C++ seems to accept the code without a typename, however I don't have too much faith in the compiler being fully standard compliant (in fact, it has many non-standard extensions, e.g. binding rvalues to non-const references).

问题:是否有任何原因使这不是C ++ 11及更高版本中的typename规则的例外?

Question: Is there any reason why this is not an exception to the typename rule in C++11 and later?

推荐答案

没有硬道理.如 ralismark说,今年讨论了一篇论文(针对C ++ 20,而不是17!).有一些担忧,也有一些反对意见:

There is no hard reason. As ralismark said, a paper was discussed this year (for C++20, not 17!). There are concerns, but also counterpoints:

  1. 这可以被视为使该语言变得不规则(如 ,已经有一个特殊的情况,例如基类名称.)
  2. 它可能会取消可能的扩展名(如
  1. It could be seen as making the language less regular (as codeshot said), but the new idea is that disambiguating typename will become rare enough as to have near-consistency in the other direction. (As has been said, there was already an exceptional case in the form of base class names.)
  2. It could foreclose possible extensions (as T.C. reported), but the extensions could have their own disambiguation rather than burdening the common case.

本文得到了有力的支持,新的规则可能会在几个月后出现在工作草案中.

The paper has strong support and the new rules will probably appear in the working draft in a few months.

这篇关于为什么仍然需要在using语句的RHS中消除类型为typename的依存类型的歧义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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