我如何公开一个库的枚举,这样我的代码就不必键入整个命名空间来使用该枚举?

How do I expose a library's enum, such that my code doesn't have to type out the entire namespace to use that enum?

我正在使用名为 spdlog 的库进行日志记录。我想围绕库构建自己的记录器,以便我可以选择添加特定于我的应用程序的 'extra' 功能。

我能够使下面的代码正常工作:

#include <spdlog/spdlog.h>

int main()
{
  spdlog::log(spdlog::level::level_enum::info, "this is an info message");
  return 0;
}

如您所见,日志记录级别可通过命名空间为 spdlog::level::level_enum 的枚举获得。

我可能过于复杂了,但是如果我创建自己的记录器 class 我是否必须期望 class 使用我的记录器在它们的记录函数调用中键入整个枚举的命名空间?

but if I create my own Logger class will I have to expect classes using my logger to type out the entire enum's namespace in their logging function calls

这是您可以使用 using 的地方。类似于:

using info = spdlog::level::level_enum::info;

这样可以防止每次需要使用时都键入整个内容。那么你只需要使用info即可。

if I create my own Logger class will I have to expect classes using my logger to type out the entire enum's namespace in their logging function calls?

我建议您 class 定义自己的 info 值,然后在需要时 map 将其内部映射到 spdlog 的 info 值。 spdlog 是 class 内部的 实现细节 ,所以不要在 class 之外公开 spdlog,如果可以的话,请将其隐藏起来。如果您愿意,这也允许您将来换出另一个记录器库,而不会破坏使用您的 class.

的代码