Numeric literal operator error

Eric picture Eric · Jan 16, 2013 · Viewed 17.4k times · Source

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

Answer

Some programmer dude picture Some programmer dude · Jan 16, 2013

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.