如何为有符号整数类型创建用户定义的文本? [英] How do I create a user-defined literal for a signed integer type?

查看:121
本文介绍了如何为有符号整数类型创建用户定义的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这篇文章发现,用户定义的参数类型允许文字类型如下:

As I discovered from this post the parameter types allowed for a user-defined literal type are as follows:

const char*
unsigned long long int
long double
char
wchar_t
char16_t
char32_t
const char*, std::size_t
const wchar_t*, std::size_t
const char16_t*, std::size_t
const char32_t*, std::size_t

我看到在该列表是 char ,这太小。如果我想这样做,该怎么办?

Well, the only signed integer I see in that list is char, which is too small. What if I wanted to do something like this:

str operator"" _i(int i) {
    return i*2;
}

然后当我写 -1000_i 我期望得到 -2000 。我如何做到这一点?

Then when I write -1000_i I expect to get -2000. How do I do this?

推荐答案

没有负的整数文字。 -1000 是将一元 - 运算符应用于文本 1000

There is no such thing as a negative integer literal. -1000 is the application of the unary - operator to the literal 1000.


然后当我写 -1000_i code> -2000 。如何这样做?

Then when I write -1000_i I expect to get -2000. How do I do this?

定义 1000_i 一元 - 给出 -2000 。您可以例如使 1000_i 具有自定义重载运算符 - 的结构类型。

Define 1000_i in such a way that applying unary - gives -2000. You might for example make 1000_i a structure type with a custom overloaded operator-.

这篇关于如何为有符号整数类型创建用户定义的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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