我们可以在没有模板的情况下使用 C++ 关键字 "typename" 吗?
can we use the C++ keyword "typename" without templates?
我在头文件中看到了这段代码。
using pointIndex = typename std::pair<std::vector<double>, size_t>;
- 我们可以在没有模板的情况下使用 "typename" 吗?
- 这里有必要使用typename吗?
- "typedef"和"typename"有什么区别?
typename
和typedef
是完全不同的东西。
typedef
是用于引入 type 别名(type def启动)。在最近的标准中,您也可以为此使用 using
。所以:
typedef int MyThing;
// or
using MyThing = int;
typename
是表示 "the next thing is a type" 的关键字。它在某些情况下处理模板时使用,包括模板声明(例如 template <typename T> void foo() { /*..*/ }
)和 to help the parser along in some situations. In the example you've given, it is valid, but redundant.
两者完全不同,因此不可互换。
我在头文件中看到了这段代码。
using pointIndex = typename std::pair<std::vector<double>, size_t>;
- 我们可以在没有模板的情况下使用 "typename" 吗?
- 这里有必要使用typename吗?
- "typedef"和"typename"有什么区别?
typename
和typedef
是完全不同的东西。
typedef
是用于引入 type 别名(type def启动)。在最近的标准中,您也可以为此使用 using
。所以:
typedef int MyThing;
// or
using MyThing = int;
typename
是表示 "the next thing is a type" 的关键字。它在某些情况下处理模板时使用,包括模板声明(例如 template <typename T> void foo() { /*..*/ }
)和 to help the parser along in some situations. In the example you've given, it is valid, but redundant.
两者完全不同,因此不可互换。