简体   繁体   English

我们可以在没有模板的情况下使用 C++ 关键字“typename”吗?

[英]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>;
  1. Can we use "typename" here without a template?我们可以在没有模板的情况下在这里使用“typename”吗?
  2. Is it necessary to use typename here?这里有必要使用 typename 吗?
  3. What is the difference between "typedef" and "typename"? “typedef”和“typename”有什么区别?

typename and typedef are completely different things. typenametypedef是完全不同的东西。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM