20

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

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;
}

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

Community
  • 1
  • 1
quant
  • 21,507
  • 32
  • 115
  • 211
  • 1
    Sidenote: `char` may or may not be a signed type depending on implementation. – eerorika May 02 '14 at 14:20
  • 3
    `char` (and `wchar_t`, `char16_t` and `char32_t` apply to single-quoted character literals. A string of digits is always an integer literal and it is always positive, because the `-` sign is _not_ part of it. – Jan Hudec May 02 '14 at 14:22

1 Answers1

37

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

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

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-.