在左值上调用用户定义的文字 [英] Invoke user-defined literal on lvalue

查看:85
本文介绍了在左值上调用用户定义的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在左值上调用用户定义的文字吗?

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

例如我想

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

// Works fine
17_xor1;

auto myint = get_something_only_availabe_at_runtime();
// Any way to use _xor1 on myint?

_xor1(myint); // Doesn't work

此外,在编译器浏览器中编译以下代码时,我很惊讶发现所有数据都在运行时解决了,尽管所有数据在编译时都可用。

Also, when compiling the following code at the compiler explorer, I was surprised to discover that it was all resolved at runtime, although all data is available at compile time. Why is that?

constexpr int operator "" _xor1(unsigned long long a) {
    return a^1; 
}

int main() {
    // This code resolves the user defined literal at runtime on gcc, 
    // msvc and clang - I don't see why I can't use the 
    // user defined literal at runtime?
    return 17_xor1;
}


推荐答案

我不确定您确实想要-正如所评论的,您最好定义一个正常的函数并调用它-但您可以使用以下方法来调用它:

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.

这篇关于在左值上调用用户定义的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆