Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C macro templace abuse

Name: Anonymous 2012-06-14 1:13

#include <stdio.h>
#define swap(T,x,y) ({T tmp = (x); (x) = (y); (y) = tmp;})

int main() {
    int x = 2, y = 5;

    swap(int, x, y);
    printf("x = %d, y = %d", x, y); //x = 5, y = 2
    return 0;
}


Is this bad practice?

Name: Anonymous 2012-06-14 1:37

>>1
Is this bad practice?
Yes. That is not a proper ENTERPRISE implementation.

void.h C11 Enterprise Extensions Version
[code]
/**
 * VOID.H C11 ENTERPRISE EXTENSIONS
 */

#ifndef VOIDEE_H
#define VOIDEE_H

#define swap(x, y) _Generic((x), \
        _Bool*: swap_bool, \
        char*: swap_char, \
        signed char*: swap_schar, \
        short*: swap_short, \
        int*: swap_int, \
        long*: swap_long, \
        long long*: swap_llong, \
        unsigned char*: swap_uchar, \
        unsigned short*: swap_ushort, \
        unsigned int*: swap_uint, \
        unsigned long*: swap_ulong, \
        unsigned long long*: swap_ullong, \
        float*: swap_float, \
        double*: swap_double, \
        long double*: swap_ldouble)(x, y)

inline void swap_bool(_Bool* x, _Bool* y) {
    _Bool t = *x;
    *x = *y;
    *y = t;
}

inline void swap_char(char* x, char* y) {
    char t = *x;
    *x = *y;
    *y = t;
}

inline void swap_schar(signed char* x, signed char* y) {
    signed char t = *x;
    *x = *y;
    *y = t;
}

inline void swap_short(short* x, short* y) {
    short t = *x;
    *x = *y;
    *y = t;
}

inline void swap_int(int* x, int* y) {
    int t = *x;
    *x = *y;
    *y = t;
}

inline void swap_long(long* x, long* y) {
    long t = *x;
    *x = *y;
    *y = t;
}

inline void swap_llong(long long* x, long long* y) {
    long long t = *x;
    *x = *y;
    *y = t;
}

inline void swap_uchar(unsigned char* x, unsigned char* y) {
    unsigned char t = *x;
    *x = *y;
    *y = t;
}

inline void swap_ushort(unsigned short* x, unsigned short* y) {
    unsigned short t = *x;
    *x = *y;
    *y = t;
}

inline void swap_uint(unsigned int* x, unsigned int* y) {
    unsigned int t = *x;
    *x = *y;
    *y = t;
}

inline void swap_ulong(unsigned long* x, unsigned long* y) {
    unsigned long t = *x;
    *x = *y;
    *y = t;
}

inline void swap_ullong(unsigned long long* x, unsigned long long* y) {
    unsigned long long t = *x;
    *x = *y;
    *y = t;
}

inline void swap_float(float* x, float* y) {
    float t = *x;
    *x = *y;
    *y = t;
}

inline void swap_double(double* x, double* y) {
    double t = *x;
    *x = *y;
    *y = t;
}

inline void swap_ldouble(long double* x, long double* y) {
    long double t = *x;
    *x = *y;
    *y = t;
}

#endif

/**
 * VOID.H C11 ENTERPRISE EXTENSIONS EXAMPLE CODE
 */

#include <voidee.h>
#include <stdio.h>

int main() {
    int x = 2, y = 5;
    swap(&x, &y);
    printf("x = %d, y = %d", x, y); //x = 5, y = 2
    return 0;
}>>1
>>1
>>1

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List