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

Pages: 1-

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

Name: Anonymous 2012-06-14 1:39

>>2
Looks like I found a new way to break Shiichan!

Name: Anonymous 2012-06-14 1:41

/**
 * 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;
}

Name: Anonymous 2012-06-14 1:45

>>2
lol

>>1
Personally, I'd just write a generic function, since it will work with arrays and structs as well (and I guess I just prefer using functions over macros). Aside from that, I don't think there's anything largely wrong with what you're doing.

#include <stdio.h>

static void *swap(void *v1, void *v2, size_t size)
{
    char *a = v1;
    char *b = v2;

    while (size--) {
        char tmp = *a;

        *a++ = *b;
        *b++ = tmp;
    }
    return v1;
}

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

    swap(&x, &y, sizeof x);
    printf("x = %d, y = %d\n", x, y);
    return 0;
}

Name: Anonymous 2012-06-14 1:49

>>5
Rename your function swapmem, and you have a winner.

Name: Anonymous 2012-06-14 1:50

>>6
Or memswap, I mean.

Name: Anonymous 2012-06-14 1:54

>>7
'mem' followed by a lower case letter is a reserved name in GNU libc.

http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html

Name: Anonymous 2012-06-14 3:03

>>7
or maybe even swapsperm

Name: Anonymous 2012-06-14 4:05

>>5
just add a little #define SWAP(x, y) swap(&(x), &(y), sizeof(x))
and you're good to go.

Name: Anonymous 2012-06-14 8:39

>>9
swaps perm? this has nothing to do with permissions

Name: Anonymous 2012-06-14 18:06

>>5
This breaks under certain conditions.

Name: Anonymous 2012-06-15 1:22

Yes, OP, it is bad practice.


    int x = 2, y = 5;
    swap(int, x++, y++); // OH SHI~

Name: Anonymous 2012-06-15 20:47

Here's how a popular C library implements it.


void generic_swap(void * a, void * b, size_t n)
{
  int counter = 0;

  for(counter = 0; counter < n; ++counter)
  {
    *( ((BYTE *) a) + counter) ^= *( ((BYTE *) b) + counter);
    *( ((BYTE *) b) + counter) ^= *( ((BYTE *) a) + counter);
    *( ((BYTE *) a) + counter) ^= *( ((BYTE *) b) + counter);
  }
}

Name: Anonymous 2012-06-16 1:32

template<class T> void swap(T& a, T& b) {
    T tmp = a;
    a = b;
    b = tmp;
}

Name: Anonymous 2012-06-16 6:18

test

Name: Anonymous 2012-06-16 21:07

茹茐䄂艰脩坥獔䎇茓䌱眕椗―當猤悘㍇᎖䡓鍉㖅熁選鐴ᢈ㈧晩傁䝷褧礢嚀ህㄔၤㄠ础㙇㍡䔳鉷餩⁹兵袂⠶嘐腴猘艁ऱ㍀䄒ጩ傖鍲馁ၔض饘癤衐䦕塄␰䕓⤕٢䥣螙䤸蔙鍷煖鉴眖ႃ癁扴ᤷ䞐⥆☦ᤵ褄荡瞂⦕禅酲ᅕ䉒噷ᆑ࢓袔ᘠ㤀梀頗逳茤⌹堹㞈⎐❆坳吃餄ᙈĹ蝂╰㊁ձ戆⠨̀焓㤷ㅧ⠰表ↈ鐄荡䐴艆ጉ劆靥㞁ᙈ牓劔猨敒ᄤ劐艖碆㘖腷肘螀戡ᆉ䜑瀀ij甧蕙У᜶憕ၴᅷ梗╁昈舁ᝄ祕厅鐃ᖙᚘ䚒卖喒ၶ睦✨祡䥆葐桰舒煕䍃慕甅㖀鑓㙐摓晤䆐㖀どጤၕ脨礠ᑙ堒䆔㘉䚕Ţ䞖ȥ圔≉效坒蕥⡠Ɓ儹㙕钕獥畕獸鐂膗蕐☇靱褅悀瀙折撑噹戆䈰馒䈆ͤ挖灗⒁妈呴灥㝆蝂ā♑′㠥捓#嚈脗餙獰桇ᝥ⍉煙ၦ艸镹㚇搷ኖ恈睅䥆匠斔ݠ遳癆畉ėॠᅵ敀ᖕ䆅挠䝖鞆ᤸ㊃薇匷┠ℤ⌄冕ቹا焕鑱ℱⅹ她蠐䐵祓脵蝠薅

Name: Anonymous 2012-06-16 21:44

圥锢䜅灰ࡉ嘑䅲急ቔ牷逑椠戴䝇炂怈ॖ杰䜲猑桘遄ᑂ☀鐀╣瞂⤡م舓襁㥀ㆈ㝲猆䕙ឃ肆鄠䅇❢፥酵㎁䌇⁖䦀ㄢ礉ᥡΔ脦炂⥢挠摥犇᠇包㉷䍣故呸⠀ន䔧鐵祧❒ց襹␇蒒倷ᆉ榕匓陔坩搸ဥ䖃块顅昗頠礅⌲䈇饒瀤V㕠䌰ᕰ疇▀䆃㆑ņ薁╵抅ԇ栱ᦃ鉢㥤奙ńٙ畃虘ࠦₗ㥄酠㥦腹䐀䔉⠈脔儑႘儉∨匣䙐⎇捲ࠓᚅ䁨牣႐唄 ㅲ䤀㍀䔒爷蝶ጤ琅ጶ咔䁁鍇䄇璘ɡᕁ楶䥠㑡犄楷蠡硷⥰㕗襠怆⑇椉呤Ɖ␦鉹䕳⡁䔁厘校㘅蝵䆕由灡艔⤖Ɔࠑፃ兒䐖㤖扳噔脠㕄醂摠⤙⅖䤰堁8逧࠰䙨偢鎕❨ᅆ卧楑ţ癣䈆㙑椢㠒ፃ㡉㙲搃呐暒怔玙呉▒唄㌸⤢ႄ煳卤朢⍇䕳ȣ搂瞉噡礰瘄䜃㠲䕸桲ᔩ㜃ሡ琒愅剣Ć猉捱覙梆瘕醑ͳ挹䒄艕悉ㄴ䜄㍦▒䕶垐♁न䌶ʗᅉ㉧猙ք灂ᝈ萲䔐᥸䐥䠷楥♠উ肁奙䄔逧⍀范䍑♰⑵䜹ܱₐ

Name: Anonymous 2012-06-16 22:31

ࡓ爇⚉啀鍓补ဈڇ茲䥷牒技晅䁧ጣ⁲急灰恥桕腇蠅瑈⒃捵預ᝠ蜥颀−鄈倧鉣    灁䕵桁⍨Ը╅炀ᚑ㡔衲Ԡ⒙Զ䍠ᘨ锉㑱茱遧鑄㡦࠰列⥴攒җ㚖㍰戨靷⍥搣睸啈瑴≢䑔眸獒馔琵․礓萩吓慕䘖卙煶癹硣㍹䤱‹癒䡇፨ᑆ䕈شΓ蔹奨膂蒅瑣虦⤷匆昳⤖㊆著饲⍘虹䂉㕵耔≔䄢睴葖╘⌣琉週璘⑱椷㍁ፒ┈㑈倠月祔䑄捡錤ᘱ鉖⌃ᢓ遴ㄅ餸蜉灂⠦Ƃ芑←聗㝃楅蔃坠灩␰❓г噆吡☆蜩Ͳ鍹昶❨▖᎖节唁癐ژ虄霈䈆㈁>褅癰̹䝷写饳͢襔̈⍆鄸艑さ鑁⑖挥▙ⅉᥧ眓卤預頢☕≈㑄ሗ䈱塡饤摹㙐蜀ɢ㤕ᙦ栶蒈䑁椑陘鑖熖ࡣ舡煤冂Ţ㍀⤇儴鑸坐ሔ碈᎗㕈す➔朂塀蔁㌧ᝈ焢ㅦ㐰ᄡ脈蜴然申㖃悓ឈづ墉䔤㠵䖐⠘蜠户喕y鍦癩襳镘⑸ᔷ悆〒舥䦙萔瑳ᥢĶ锉艓䐦⡙႐蘹⒉ݒ―ᥱㅷ托鑒⌀收瑥犒⍑䁓䍆頡頕脨鞔

Name: Anonymous 2012-06-16 23:12

⌔昧䤖をv䝂䡣ↃᡸȂᕄ⌖㡹䔐栖荁㝣舐蘷芈う搵捣㥧㖓॔⎃ᄨ逨䡗范˜镘褦S㙥㘁→᥅爲㝢敠䐔⌹⠹≉祖祒⅔䙨ͩ构蠢慔礦腂睲坦ᔷ煳㑈ᅤ≲瑹̲ƙᡨ鍴䈁霵熖䂁ᙑ圣䍉ᤉ瘕钐瘵瑤瘄蕅饲㠩砆瀳塂硘栳祤栆✴慙戗慀坸蔃㌸䜵阅噥呠ቕࠆࡱ猧㑹㝇➕䅄妕ጃⅨ癓ㄕ䕙G瑷㝖瑶禄⢒猶木肄ᆉᢆ䈴炐ㅸ挄蕩㤨䀉ᕘ唄ɥƗ獘鑒瑨ᦆ嘀煓搡ᤓ㠧ↄ䌲㤹㈷镔片搈玗疖禐❀䜃ሶ鍂㥸撈ʘᦂ㑗㊖鎑ن堀脃聙牴㖆攐鍂ԗ襹夔祘鉁䦅䥱椂眄ጐ㦕㈳✱镤舔荶硱㜃ܵ码猧㢒ㅠ餸蒐ㄨи祄陃䥅ԡ礶薘舵荘⥇╨➅䑉㌢战䙷蚇虷㘴⌡ᄙ舲脔ᦙ⌧扢≁最℔鑷㦅教斅ᘁↅ睧瞁❢蕴㚆ᚔ䑈ㅈΓ芇坢劘̰条冀搖⡁ㅆጆ⑴ࡗّք吀䠵ㄲؕ悇⍧恒圔桠耈Ԓ嘅֐袐猴荙袙瑹р‡眵፨甈兙䤤ᝩ吤⥶䉠儁ጐ瑲愸兴♉ॠ猤噲⍗

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