mercredi 24 août 2016

Cleaner or Elegent way to convert string literal to hexa-decimal [on hold]

I was through a legacy code-base and saw following -- an ugly way to create unique hexa-decimal enums

This work around clutters the build messages, can we achieve similar effect with cleaner syntax without cluttering the build messages.

#include <iostream>

enum t
{
    XX = 'AA', // 0x4141  or  0100 0001 0100 0001
};

int main()
{
    std::cout<<XX<<std::endl;
}

It creates an enum with value 0x4141, but compiler shows warnings.

Compile and run will spit following.

main.cpp:8:10: warning: multi-character character constant [-Wmultichar]
    XX = 'AA', // 0x4141  or  0100 0001 0100 0001
         ^
1 warning generated.

16705

Demo on coliru

Aucun commentaire:

Enregistrer un commentaire