如何在c ++ 11中定义否定的UDL(不允许使用?)? [英] How do I define a negative UDL in c++11 (are they disallowed?)?

查看:124
本文介绍了如何在c ++ 11中定义否定的UDL(不允许使用?)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至不确定是否允许使用否定的用户定义文字。如果没有,为什么将它们排除在外?

I'm not even sure if negative User-Defined Literals are allowed. If not, why were they left out?

例如,我想使用:

auto money_i_owe_jack = -52_jpy;

这是我使用gcc 4.7.2尝试过的操作:

This is what I tried using gcc 4.7.2:

constexpr int64_t operator "" _jpy(long long l)
{
  return static_cast<int64_t>(l);
}

错误

Test_udl.cpp:60:47: error: ‘constexpr int64_t operator"" _jpy(long long int)’ has invalid argument list


推荐答案

整数文字必须接受为 unsigned long long code>。负号不是文字的一部分,而是在返回值之后应用。

Integer literals need to be accepted as unsigned long long. The negative sign is not part of the literal, it is applied after the fact, to the returned value.

constexpr int64_t operator "" _jpy(unsigned long long l)
{
  return static_cast<int64_t>(l);
}

这篇关于如何在c ++ 11中定义否定的UDL(不允许使用?)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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