2

The following code is taken from cpp-netlib but I've seen similar declarations elsewhere.

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. Searching with the word 'type' in the search box is throwing up far too many unrelated results to find an answer.

TIA

ksl
  • 4,519
  • 11
  • 65
  • 106

3 Answers3

4

It's a workaround since C++98 (and also C++03) does not allow to template typedefs. If you look into server_base class, you'll see a typedef called type, whose type depends on the template parameters of server_base. FYI, in C++11 you can use alias declarations. See C++ template typedef

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
2

It is probably a typedef inside a templated class/struct that defines some type. 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

Adrian Lis
  • 647
  • 4
  • 20
1

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. This way is widely used in C++ standard library, for example std::integral_constant:

#include <type_traits>

typedef std::integral_constant<int, 2> two_t;

two_t::type my_constant; // <--- simplified 
masoud
  • 55,379
  • 16
  • 141
  • 208