以下代码取自 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) {}
};
请有人解释一下在声明中使用 ::type
的意义是什么。在搜索框中使用“类型”一词进行搜索会抛出太多不相关的结果,无法找到答案。
TIA
最佳答案
这是一种变通方法,因为 C++98(以及 C++03)不允许模板类型定义。
如果查看 server_base 类,您会看到一个名为 type
的 typedef
,其类型取决于 server_base 的模板参数。仅供引用,在 C++11 中,您可以使用别名声明。参见 C++ template typedef
关于c++ - c++声明中的::type是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072044/