I have defined the following user-defined literal in MyLiteral.h
:
namespace my_literals {
constexpr uint64_t operator"" _nanoseconds(unsigned long long int value) {
return value*1000;
}
}
Now I could use the operator in another header SomeComponent.h
:
using namespace my_literals;
namespace foo {
constexpr uint64_t timeout = 10_nanoseconds;
}
However, I don't want to pollute the scope by using namespace my_literals
, because this would provide the literal definition to all *.cpp
files which include SomeComponent.h
.
How can I avoid this? constexpr uint64_t timeout = my_literals::10_nanoseconds;
gives expected unqualified-id before numeric constant in g++.
In C++17, with constexpr lambda, you may do:
namespace foo {
constexpr uint64_t timeout = []{ using namespace my_literals; return 10_nanoseconds; }();
}
as alternative to (C++11 and higher):
namespace foo {
constexpr uint64_t timeout = my_literals::operator""_nanoseconds(10);
}
or
namespace foo {
namespace detail
{
using namespace my_literals;
constexpr uint64_t timeout = 10_nanoseconds;
}
using detail::timeout;
}
You can get around this by calling the operator explicitly:
namespace foo {
constexpr uint64_t timeout = my_literals::operator""_nanoseconds(10);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With