Reputation: 43
I have an operator defined in a namespace as follows:
namespace Foo {
class Bar {
public:
Bar(double val): baz(val) {}
// Rest of my object here
private:
double baz;
};
namespace Qux {
Bar operator ""
_quux(long double opd) {
return Bar(opd / 10);
}
}
}
int main() {
using namespace Foo::Qux;
std::cout << "100_quux" << std::endl;
}
How do I use the operator without introducing the Foo::Qux
namespace into my main()
scope?
Upvotes: 3
Views: 874
Reputation: 180415
You can't qualify the namespace for user define literals like
std::cout << 100.0Foo::Qux::_quux << std::endl
But what you can do is use a using statement to import just the literal operator into main
using
using Foo::Qux::operator""_quux;
and you would use it like
std::cout << 100.0_quux << std::endl;
You could also call the operator manually like
std::cout << Foo::Qux::operator""_quux(100.) << std::endl;
Another option would be to place your user define literals into a namespace called literals
and then you can just import that into main. That would look like
namespace Foo {
class Bar {
public:
Bar(double val): baz(val) {}
// Rest of my object here
private:
double baz;
};
namespace Qux {
inline namespace literals {
Bar operator ""_quux(long double opd) {
return Bar(opd / 10);
}
}
// other Qux Members
}
}
int main() {
using namespace Foo::Qux::literals;
std::cout << 100.0_quux << std::endl;
}
Note that literals is an inline namespace so that Qux
can still access the members without additional qualification.
Upvotes: 13