简体   繁体   English

为什么我不需要在 C++20 中的依赖类型之前指定“typename”?

[英]Why don't I need to specify “typename” before a dependent type in C++20?

This bit of code compiled in C++20 (using gcc 10.1) without using the typename keyword before the dependent type std::vector<T>::iterator .这段代码在 C++20 中编译(使用 gcc 10.1),在依赖类型std::vector<T>::iterator之前没有使用typename关键字。 Why does it compile?为什么会编译?

#include <vector>

template<typename T>
std::vector<T>::iterator // Why does this not require "typename" before it?
f() { return {}; }

int main() {
    auto fptr = &f<int>;
}

code playground代码游乐场

One of the new features in C++20 is Down with typename . C++20 的新特性之一是Down with typename

In C++17, you had to provide the typename keyword in nearly all dependent contexts to disambiguate a type from a value.在 C++17 中,您必须在几乎所有相关上下文中提供typename关键字,以消除类型与值的歧义。 But in C++20, this rule is relaxed a lot.但是在 C++20 中,这个规则放宽了很多。 In all contexts where you need to have a type, the typename keyword is no longer mandatory.需要类型的所有上下文中, typename关键字不再是强制性的。

One such context is the return type of a function in class scope, as in your example.一个这样的上下文是 class scope 中 function 的返回类型,如您的示例所示。 Others include the type in a member declaration, the type on the right-hand side of a using declaration, the parameter declaration of a lambda, the type you're passing to static_cast , etc. See the paper for the full list.其他包括成员声明中的类型、using 声明右侧的类型、lambda 的参数声明、传递给static_cast的类型等。有关完整列表,请参阅论文。


Nearly all because base-specifiers and mem-initializer-ids were always excluded, as in: 几乎都是因为 base-specifiers 和 mem-initializer-ids 总是被排除在外,如下所示:

template <typename T> struct X : T::type  { }; // always ok

This is okay because, well, that needs to be a type.这没关系,因为,嗯,这需要是一种类型。 The paper simply extends this logic (well, it has to be a type, so let's just assume it's a type) to a lot more places that have to be types.这篇论文只是将这个逻辑(嗯,它必须是一个类型,所以我们假设它是一个类型)扩展到更多必须是类型的地方。

From the reference , from c++20, in contexts where the dependent name is unambiguously a typename, the typename keyword is no longer needed.从 c++20 的引用中,在从属名称明确为类型名的上下文中,不再需要typename关键字。 In particular:尤其是:

A qualified name that is used as a declaration specifier in the (top-level) decl-specifier-seq of:在以下的(顶级)decl-specifier-seq 中用作声明说明符的限定名称:

a simple declaration or function definition at namespace scope命名空间 scope 的简单声明或 function 定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 C++20 中的这种情况下仍然需要 `typename` 前缀? - Why is `typename` prefix still required in such a case in C++20? 为什么C ++ 20模板lambda使用typename关键字? - Why are C++20 template lambdas using typename keyword? 在 C++20 中使用 typename 需要 / 概念? - Using typename in C++20 requires / concept? 为什么 C++20 中的容器没有 .ssize 成员 function? - Why don't containers in C++20 have .ssize member function? 为什么在 C++20 之前 std::swap 没有标记为 constexpr ? - Why isn't std::swap marked constexpr before C++20? 我如何要求(在 C++20 概念中)类型 T 是对浮点类型的引用? - How can I require (in a C++20 concept) that a type T be a reference to a floating point type? 从属模板名称和 C++20 ADL - Dependent template names and C++20 ADL C++20 约束 - 将类型名 TFunction 限制为函数(具有特定签名) - C++20 Constraints - Restrict typename TFunction to be a function (with certain signature) 是否应该在 c++20 中禁止有关缺少类型名的警告? - Should warnings about missing typename supressed in c++20? c++20为什么我不能像其他视图函数一样“管道”到views::reverse? - c++20 why can't I "pipe" into views::reverse like the other views functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM