[英]can we use the C++ keyword “typename” without templates?
I saw this code in a header file.我在 header 文件中看到了这段代码。
using pointIndex = typename std::pair<std::vector<double>, size_t>;
typename
and typedef
are completely different things. typename
和typedef
是完全不同的东西。
typedef
is a keyword used to introduce a type alias (a type def inition). typedef
是用于引入类型别名(类型定义初始化)的关键字。 In recent standards you can also use using
for this.在最近的标准中,您也可以使用
using
。 So:所以:
typedef int MyThing;
// or
using MyThing = int;
typename
is a keyword that says "the next thing is a type". typename
是一个关键字,表示“接下来的事情是类型”。 It's used when dealing with templates in some situations, both in template declarations (eg template <typename T> void foo() { /*..*/ }
) and to help the parser along in some situations .它用于在某些情况下处理模板,包括模板声明(例如
template <typename T> void foo() { /*..*/ }
) 以及在某些情况下帮助解析器。 In the example you've given, it is valid, but redundant .在您给出的示例中,它是有效的,但是是多余的。
The two things are entirely different, and thus not interchangeable.这两件事完全不同,因此不能互换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.