0

I am learning how the websocketpp header-only library works, and I was quite perplexed by the lib::error_code type that is sprinkled everywhere. Xcode would only reveal to me that the declaration of the type is in the <system_error> header, which was somewhat confusing, because I saw that lib is a websocketpp namespace.

Then eventually I find this in websocketpp/common/system_error.hpp:

namespace websocketpp {
namespace lib {

#ifdef _WEBSOCKETPP_CPP11_SYSTEM_ERROR_
    using std::error_code;

This is a using inside of a namespace, which is not something that I understood.

What does this do? Does this simply alias websocketpp::lib::error_code to be std::error_code?

If so, why is this not delared as typedef std::error_code error_code? That would make more sense to me.

Steven Lu
  • 36,733
  • 50
  • 179
  • 328

2 Answers2

1

The two are almost identical, cf. What is the difference between 'typedef' and 'using' in C++11? with a relevant standard quote.

That said, one user found a subtle difference concerning the grammar, cf. Konstantin Oznobihin's answer in What are the differences between typedef and using? (you can't use an "Elaborated type specifier" with the typedef name, but you can with a name introduced with a using declaration). Cf. par. 7.1.6.3 of the standard draft.

Community
  • 1
  • 1
Peter - Reinstate Monica
  • 12,309
  • 2
  • 29
  • 52
  • Like I said in my question, I tried replacing the line with a `typedef` and it seems like the code also compiles. So it has the same effect as used here. – Steven Lu Mar 14 '14 at 07:18
  • You are right, I completely rewrote my answer. An "Elaborated type specifier" is the C way of declaring struct vars, like "struct T t;". That's usually not necessary in C++ because the class name itself is already the type name, but it's still possible (removing that would break ported C code). – Peter - Reinstate Monica Mar 14 '14 at 07:56
1

(WebSocket++ library author here) using allows aliasing a template class into a namespace in C++98. typedef (pre-c++11) requires a fully specified type.

In the case of lib::error_code this is irrelevant because it is not a template class, but the general pattern that the namespace websocketpp::lib uses to alias between boost:: and std:: requires that it work for template classes as well (e.g. lib::shared_ptr). For consistencies sake, all aliases in websocketpp::lib use the using syntax rather than switching between typedef for non-template classes and using for templates.

zaphoyd
  • 2,412
  • 1
  • 13
  • 19
  • Cool, thanks. I agree with that reasoning. (It just made it slightly harder for me to hunt down the declaration, though now that I know this, i'll know what to look for next time) – Steven Lu Mar 26 '14 at 01:22