是否有可能限制文字运算符的范围?
我想定义一些后缀来使一些事情更容易指定,但这只与直接与特定类或其子类相关的事情有关。使用此类的其他代码的 namespace 不应受此影响。
最佳答案
使用命名空间。 wandbox example
namespace my_lits
{
int operator ""_aaa(const unsigned long long x)
{
return x + 1;
}
}
int main()
{
{
using namespace my_lits;
std::cout << 100_aaa << "\n";
}
{
// Will not compile!!!
std::cout << 100_aaa << "\n";
}
}
Is there a way to make this namespace always visible then in class' scope?
不可能将命名空间引入类的范围内。引用this问题和this other one了解更多详情。
此外,UDL cannot be declared in class scope .
关于c++ - 如何限制文字运算符的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788462/