3

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?

7
  • I thought about using Foo::Qux::operator_quux (100) but that didn't seem to work. Commented Jul 28, 2021 at 15:06
  • I would suggest you to get the operator working without namespaces first, because the namespace isnt the major problem in your code. Also there is no operator<< for Bar Commented Jul 28, 2021 at 15:16
  • I've never seen operator "" in the wild before. Saw it had been added in C++11, but never looked into it. Now that I have, that's a <expletive deleted> to find documentation on. cppreference doesn't even seem to cover it. Commented Jul 28, 2021 at 15:18
  • The fully qualified name of the operator is Foo::Qux::operator""_quux, not Foo::Qux::operator_quux. You can invoke it with that, or you can also do using Foo::Qux::operator""_quux; to use the literal operator without using the rest of the namespace. Commented Jul 28, 2021 at 15:18
  • 1
    @user4581301 cppreference has it under user-defined literals Commented Jul 28, 2021 at 15:18

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Since Foo::Qux only contains the literal it already serves the purpose of separating the literals from the rest. It could be renamed into literals though.
@TedLyngmo That was my though, but the last sentence in the Q makes me think that Qux has other members that just aren't shown here.
I've accepted your answer(thanks!) - can you also add @NathanPierson's comment into it for completeness?
@cshastry You're welcome. I've also added in the manual call version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.