[英]What does ::type in c++ declaration mean?
The following code is taken from cpp-netlib but I've seen similar declarations elsewhere. 以下代码取自cpp-netlib,但我在其他地方看到了类似的声明。
template <class Handler>
struct server : server_base<tags::http_server, Handler>::type
{
typedef typename server_base<tags::http_server, Handler>::type server_base;
typedef server_options<tags::http_server, Handler> options;
explicit server(options const &options) : server_base(options) {}
};
Please can someone explain what the significance is of using ::type
in the declaration. 请有人解释在声明中使用::type
的重要性。 Searching with the word 'type' in the search box is throwing up far too many unrelated results to find an answer. 在搜索框中使用“类型”一词进行搜索会产生太多无关的结果,无法找到答案。
TIA TIA
It's a workaround since C++98 (and also C++03) does not allow to template typedefs. 这是一个解决方法,因为C ++ 98(以及C ++ 03)不允许模板typedef。 If you look into server_base class, you'll see a typedef
called type
, whose type depends on the template parameters of server_base. 如果查看server_base类,您将看到一个名为type
的typedef
,其类型取决于server_base的模板参数。 FYI, in C++11 you can use alias declarations. 仅供参考,在C ++ 11中,您可以使用别名声明。 See C++ template typedef 请参阅C ++模板typedef
It is probably a typedef inside a templated class/struct that defines some type. 它可能是模板化类/结构中的typedef,它定义了某种类型。 In order to see what it really is either look in sever_base template definition or if using Visual Studio just hover mouse over type - it should show the underlying type as a tooltip 要查看它实际上是什么,请查看sever_base模板定义,或者如果使用Visual Studio将鼠标悬停在类型上 - 它应该将基础类型显示为工具提示
Most of the time, it is a tool for simplifying to access/expose a inner type. 大多数情况下,它是一种简化访问/公开内部类型的工具。 Instead of writing a long declaration of a template class, you can just access the type by ::type
. 您可以通过::type
访问类型,而不是编写模板类的长声明。 This way is widely used in C++ standard library, for example std::integral_constant
: 这种方式广泛用于C ++标准库,例如std::integral_constant
:
#include <type_traits>
typedef std::integral_constant<int, 2> two_t;
two_t::type my_constant; // <--- simplified
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.