【发布时间】:2018-11-29 06:38:09
【问题描述】:
我在MyLiteral.h 中定义了以下用户定义的文字:
namespace my_literals {
constexpr uint64_t operator"" _nanoseconds(unsigned long long int value) {
return value*1000;
}
}
现在我可以在另一个标题 SomeComponent.h 中使用运算符:
using namespace my_literals;
namespace foo {
constexpr uint64_t timeout = 10_nanoseconds;
}
但是,我不想被using namespace my_literals 污染范围,因为这将为包括SomeComponent.h 在内的所有*.cpp 文件提供文字定义。
我怎样才能避免这种情况? constexpr uint64_t timeout = my_literals::10_nanoseconds; 在 g++ 中给出数字常量之前的预期 unqualified-id。
【问题讨论】:
-
对不起,我重新打开了,因为重复的答案没有在头文件中的限制,但事实证明解决方案是相同的。我将其作为副本重新关闭。
-
我认为这根本不是重复的。这是涉嫌重复的更受限制的情况,需要不同的解决方案。另一个问题中接受的答案(此时只有一个)使用了不能在标头中使用的范围和
using namespace声明。
标签: c++ c++11 c++14 constexpr user-defined-literals