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.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
  • 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`. – Hatted Rooster Sep 30 '16 at 09:47
  • @GillBates hm - that at least hides it by default. is there a way to make this namespace always visible then in class' scope? – vlad_tepesch Sep 30 '16 at 09:52

1 Answers1

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.

Community
  • 1
  • 1
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416