0

I don't even know how to search for that but I tried to test it, still could not understand it. What is the code below means:

boost::asio::io_service::id service::id;

Got it from: MaidSafe-CRUX at GitHub

Ozgur Bagci
  • 768
  • 11
  • 25
  • Possible duplicate of [What is namespace used for, in C++?](https://stackoverflow.com/questions/5333568/what-is-namespace-used-for-in-c) –  Apr 26 '18 at 07:25
  • 2
    Not a bad question, but needs a better title to halt the downvotes, though I can't think of one. Perhaps _What does this nested namespace definition do?_ – acraig5075 Apr 26 '18 at 07:50
  • @acraig5075 Maybe works better. I will change the title. Thanks. – Ozgur Bagci Apr 26 '18 at 08:56

1 Answers1

5

:: is the scope resolution operator. The name on the left-hand side denotes a scope; it can be either a namespace name or a class name. The name on the right-hand side denotes a member of that scope.

In your case, there seems to be a class maidsafe::crux::detail::service, which contains a static member id of type boost::asio::io_service::id. In many cases, static members need an out-of-class definition, which is being provided by the code you've posted.

It can be parsed as any other C++ declaration: it declares something named service::id of type boost::asio::io_service::id. Let's look at the individual components.

Type:

  • boost is a namespace (in global scope)
  • asio is a namespace inside boost
  • io_service is either a namespace or class inside boost::asio
  • id is a type inside boost::asio::io_service

Name:

  • service is a class name (probably inside namespace maidsafe::crux::detail)
  • id is a static member inside maidsafe::crux::detail::service.

Addendum

While it does not appear in this question, there's a related syntax you may sometimes encounter in more modern C++ code:

namespace maidsafe::rux::detail
{

boost::asio::io_service::id service::id;

}

This has been introduced in C++17 as a short-hand for opening multiple namespaces on one line. It's exactly equivalent to the code in the question.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 2
    `service` *might* be a namespace, and can exist in any of `::`, `::maidsafe`, `::maidsafe::run` or `::maidsafe::run::detail`. You are *probably* right about it however – Caleth Apr 26 '18 at 07:37
  • @Caleth Indeed. Or it could be a class in any of the namespaces you mention. But I feel it's quite improbable, and this issue's level doesn't suggest a need for super-precise, unlikely, potentially distracting details. – Angew is no longer proud of SO Apr 26 '18 at 07:43
  • @Angew Agreed with your reasoning, otherwise, I was going to throw some references to qualified-name and qualified-id here. – Tanveer Badar Apr 26 '18 at 07:57