I have an enum type, for example:

enum signum {negative = -1, positive = 1, zero = 0, inward = -1, outward = 1, indefinite = 2};

I want to use at least one of the following castings:

signum s = positive;
double x,y;
x = double(s);
y = (double)s;

Dani AI

Generated

Short answer: you cannot change what (double)s means for a plain enum. In C++, user-defined conversions must be member functions of a class/struct/union. Enums cannot have members, so you cannot overload the cast operator for them. That is why @Ancient Dragon sees the default integral-to-double conversion work, and why suggested a function.

If you want indefinite to become NaN while the others map to their obvious values, write a small conversion utility. It is clear, testable, and avoids surprising implicit casts.

#include <limits>

enum signum { negative = -1, positive = 1, zero = 0,
              inward = -1, outward = 1, indefinite = 2 };

inline double to_double(signum s) {
    using std::numeric_limits;
    switch (s) {
        case negative:
        case inward:   return -1.0;
        case positive:
        case outward:  return  1.0;
        case zero:     return  0.0;
        case indefinite:
        default:       return numeric_limits<double>::quiet_NaN();
    }
}

If you truly want cast-like syntax, wrap the enum in a tiny class and provide a conversion operator there (this is essentially what hinted at). Keep it explicit to avoid accidental arithmetic when a signum sneaks into math code:

struct Signum {
    signum v;
    explicit Signum(signum x) : v(x) {}
    explicit operator double() const { return to_double(v); }  // C++11+
};

// usage
Signum s{positive};
double x = static_cast<double>(s);

Notes:

  • Duplicated enumerators (inward/negative, outward/positive) are fine because they share the same numeric value; the switch above handles that explicitly.
  • If you need portability for NaN, you can check std::numeric_limits<double>::has_quiet_NaN and fall back to a sentinel policy, but on modern platforms quiet_NaN() is typically available.

Recommended Answers

All 9 Replies

Worked ok with my compiler (vc++ 2010 express)

Worked OK? But what?
I do not define anything!
It was just a simple example,
but what if I want to convert other floating point numbers,
nut just 0, 1, -1,
for example I want to convert signum == 2 -> double NaN,
so I want to define a casting function:
double(signum s)

I mean: user defined casting, not the default!

Worked OK? But what?
I do not define anything!
It was just a simple example,

Sure you did

#include <iostream>

enum signum {negative = -1, positive = 1, zero = 0, inward = -1, outward = 1, indefinite = 2};

int main()
{

signum s = positive;
double x,y;
x = double(s);
y = (double)s;

}

>so I want to define a casting function:
>double(signum s)

So do it. We're not talking about rocket science here:

double to_double(signum s);

Or are you going to complain that double(s) is so vastly superior to to_double(s) that you absolutely must have a user-defined cast that uses exactly the same syntax as the native cast? :icon_rolleyes:

commented: Quite. +20

It is not possible to define "real" casting?
I don't want to define to_double function.
Instead of enum, the casting of a class is allowed, as I know,
I can just define member operator double();

>It is not possible to define "real" casting?
What I see here is the difference between a professional attitude and an amateur attitude. Yes, it is possible, but it's less clear, takes longer to implement, requires more effort to maintain, and doesn't really buy you anything for the added complexity. One with an amateur attitude will do it anyway while one with a professional attitude will crank out something simple in a few minutes (a function, perhaps?) and spend valuable time on more important things.

Can you see the difference?

Yes, I can, I understand you, but please answer me, what is the syntax to do it my way?

>what is the syntax to do it my way?
Write a class that simulates an enumeration and overload the double operator for implicit conversions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.