Search code examples
c++c++11user-defined-literals

How do I define a negative UDL in c++11 (are they disallowed?)?


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

For example, I would like to use:

auto money_i_owe_jack = -52_jpy;

This is what I tried using gcc 4.7.2:

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

ERROR

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

Solution

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