0

Is there a possibility to limit the scope of literal operators?

I would like to define some postfixes to make some things easier to specify, but this only has relevance to things directly connected to the specific class or the their child classes. Name spaces of other code that uses this classes should not be affected by this.

2
  • 1
    Just stick it inside a namespace and use using name::name::morenames; whenever you need it, it's what chrono does with std::literals::chrono_literals. Sep 30, 2016 at 9:47
  • @GillBates hm - that at least hides it by default. is there a way to make this namespace always visible then in class' scope? Sep 30, 2016 at 9:52

1 Answer 1

1

Use a 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?

It is not possible to bring a namespace into scope inside a class. Refer to this question and this other one for more details.

Furthermore, UDLs cannot be declared in class scope.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.