Using UDL in header outside function scope

Viewed 180

If we want to use some UDL we need to use the corresponding namespace:

auto foo()
{
   using namespace std::literals::chrono_literals;

   std::chrono::milliseconds interval = 1s;
}

which is all right and well because the introduced namespace is localized to the function.

But I haven't found a solution to use them outside of a function scope (e.g. in-class initializer or function default argument) without polluting the enclosing namespace:

// 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} {}
};
}

Is there a better way to use UDL here?

2 Answers
Related