How to achieve function overloading in C?

前端 未结 14 2644
清歌不尽
清歌不尽 2020-11-22 03:16

Is there any way to achieve function overloading in C? I am looking at simple functions to be overloaded like

foo (int a)  
foo (char b)  
foo (float c , i         


        
14条回答
  •  佛祖请我去吃肉
    2020-11-22 04:15

    The following approach is similar to a2800276's, but with some C99 macro magic added:

    // we need `size_t`
    #include 
    
    // argument types to accept
    enum sum_arg_types { SUM_LONG, SUM_ULONG, SUM_DOUBLE };
    
    // a structure to hold an argument
    struct sum_arg
    {
        enum sum_arg_types type;
        union
        {
            long as_long;
            unsigned long as_ulong;
            double as_double;
        } value;
    };
    
    // determine an array's size
    #define count(ARRAY) ((sizeof (ARRAY))/(sizeof *(ARRAY)))
    
    // this is how our function will be called
    #define sum(...) _sum(count(sum_args(__VA_ARGS__)), sum_args(__VA_ARGS__))
    
    // create an array of `struct sum_arg`
    #define sum_args(...) ((struct sum_arg []){ __VA_ARGS__ })
    
    // create initializers for the arguments
    #define sum_long(VALUE) { SUM_LONG, { .as_long = (VALUE) } }
    #define sum_ulong(VALUE) { SUM_ULONG, { .as_ulong = (VALUE) } }
    #define sum_double(VALUE) { SUM_DOUBLE, { .as_double = (VALUE) } }
    
    // our polymorphic function
    long double _sum(size_t count, struct sum_arg * args)
    {
        long double value = 0;
    
        for(size_t i = 0; i < count; ++i)
        {
            switch(args[i].type)
            {
                case SUM_LONG:
                value += args[i].value.as_long;
                break;
    
                case SUM_ULONG:
                value += args[i].value.as_ulong;
                break;
    
                case SUM_DOUBLE:
                value += args[i].value.as_double;
                break;
            }
        }
    
        return value;
    }
    
    // let's see if it works
    
    #include 
    
    int main()
    {
        unsigned long foo = -1;
        long double value = sum(sum_long(42), sum_ulong(foo), sum_double(1e10));
        printf("%Le\n", value);
        return 0;
    }
    

提交回复
热议问题