12

Why does this code:

constexpr float operator "" _deg(long double d) {
    // returns radians
    return d*3.1415926535/180;
}

static const float ANGLES[] = {-20_deg, -10_deg, 0_deg, 10_deg, 20_deg};

Produce 5 of these errors:

error: unable to find numeric literal operator 'operator"" _deg'

I am using GCC 4.7.3. (arm-none-eabi-g++, with the -std=c++0x flag).

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Eric
  • 95,302
  • 53
  • 242
  • 374

2 Answers2

13

It seems GCC doesn't do type conversions with user-defined literals, so e.g. the -10 in -10_deg is considered to be an integer.

Add .0 to all numbers and it should hopefully work:

static const float ANGLES[] = {-20.0_deg, -10.0_deg, 0.0_deg, 10.0_deg, 20.0_deg};

Of course, you can also add another operator function taking int as argument.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • No, but I tried something similar and it seemed to solve the problem. You're right - GCC is not doing type conversion here – Eric Jan 16 '13 at 10:21
8

Adding the definition

constexpr float operator "" _deg(unsigned long long d) {
    // returns radians
    return d*3.1415926535/180;
}

makes it work.

Eric
  • 95,302
  • 53
  • 242
  • 374
  • Yep, For numeric literals, the type of the cooked literal is either `unsigned long long` for integral literals or `long double` for floating point literals. [wikipedia](http://en.wikipedia.org/wiki/C%2B%2B11#User-defined_literals) – yiding Jan 16 '13 at 10:21