<分区>
如果我们想使用一些 UDL,我们需要使用相应的命名空间:
auto foo()
{
using namespace std::literals::chrono_literals;
std::chrono::milliseconds interval = 1s;
}
这很好,因为引入的命名空间已本地化到函数。
但我还没有找到在函数范围之外使用它们(例如类内初始化程序或函数默认参数)而不污染封闭命名空间的解决方案:
// this is a header
namespace my_ns
{
// I would like to avoid this:
// using namespace std::literals::chrono_literals;
struct Foo
{
// can't have a using directive at class scope:
// using namespace std::literals::chrono_literals;
// I want to do this
std::chrono::milliseconds interval = 1s;
// I want to pretty pretty pretty please do this:
Foo(std::chrono:milliseconds interval = 1s) : interval{interval} {}
};
}
在这里使用 UDL 有更好的方法吗?