Invoke user-defined literal on lvalue

后端 未结 2 1550
北海茫月
北海茫月 2021-01-21 02:33

Is there any way to invoke a user defined literal on lvalues?

e.g I would like to

int operator \"\" _xor1(int a) { return a^1; }

// Works fine
17_xor1;
         


        
相关标签:
2条回答
  • 2021-01-21 03:00

    I'm not sure you really want to - as commented, you're probably better off defining a normal function and calling that - but you can call it using:

    operator""_xor1(myInt);
    

    See User-defined literals for more information.

    0 讨论(0)
  • 2021-01-21 03:04

    No, you cannot invoke a user-defined literal on a variable populated at runtime. Nor do you need to. Just define a standalone function that different pieces of code can invoke when needed, eg:

    template <typename T>
    T do_xor1(T a) { return a^1; }
    
    int operator "" _xor1(unsigned long long a) { return do_xor1(a); }
    
    // Works fine
    17_xor1;
    
    auto myint = get_something_only_availabe_at_runtime();
    do_xor1(myint);
    
    0 讨论(0)
提交回复
热议问题