adding two structs representing different types together

Viewed 501

I have a struct like so:

typedef enum any_type{
    ANY_TYPE_CHAR,
    ANY_TYPE_UCHAR,
    ANY_TYPE_SHORT,
    ANY_TYPE_USHORT,
    ANY_TYPE_INT,
    ANY_TYPE_UINT,
    ANY_TYPE_LONG,
    ANY_TYPE_ULONG,
    ANY_TYPE_FLOAT,
    ANY_TYPE_DOUBLE,
} any_type;

typedef struct any{
    any_type type;
    union{
        char as_char;
        unsigned char as_uchar;
        short as_short;
        unsigned short as_ushort;
        int as_int;
        unsigned int as_uint;
        long as_long;
        unsigned long as_ulong;
        float as_float;
        double as_double;
    };
} any;

which represents a variable which is one of the types specified. There are four possible operations on these structs, which are addition, subtraction, multiplication and division.

My question is, is there an efficient way to do these operations on them without doing lots of if statements for every case? That would result in 4 * 10 * 10 = 400 if/else if statements for these four operations alone, which is certainly not efficient!

My code is in C

Thanks in advance.

4 Answers
Related