C++ 使用命名空间中的符号 [英] C++ Using a symbol from a namespace
问题描述
我正在学习 websocketpp
仅标头库的工作原理,我对到处散布的 lib::error_code
类型感到非常困惑.Xcode 只会向我显示类型的声明在 <system_error>
标头中,这有点令人困惑,因为我看到 lib
是一个 websocketpp 命名空间.
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.
然后我最终在 websocketpp/common/system_error.hpp
中找到了这个:
Then eventually I find this in websocketpp/common/system_error.hpp
:
namespace websocketpp {
namespace lib {
#ifdef _WEBSOCKETPP_CPP11_SYSTEM_ERROR_
using std::error_code;
这是一个命名空间内的using
,这不是我理解的.
This is a using
inside of a namespace, which is not something that I understood.
这是做什么的?这是否只是将 websocketpp::lib::error_code
别名为 std::error_code
?
What does this do? Does this simply alias websocketpp::lib::error_code
to be std::error_code
?
如果是这样,为什么不将其声明为 typedef std::error_code error_code
?这对我来说更有意义.
If so, why is this not delared as typedef std::error_code error_code
? That would make more sense to me.
推荐答案
(此处为 WebSocket++ 库作者)using
允许将模板类别名为 C++98 中的命名空间.typedef
(c++11 之前的)需要一个完全指定的类型.
(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.
对于 lib::error_code
这无关紧要,因为它不是模板类,而是命名空间 websocketpp::lib
用于别名的一般模式boost::
和 std::
之间要求它也适用于模板类(例如 lib::shared_ptr
).为了一致性,websocketpp::lib
中的所有别名都使用 using
语法,而不是在非模板类的 typedef
和 之间切换使用
作为模板.
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.
这篇关于C++ 使用命名空间中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!