Boost logo

Boost :

Subject: Re: [boost] Is there interest in a library for string-convertible enums?
From: Artyom Tokmakov (artyom.tokmakov_at_[hidden])
Date: 2014-10-29 18:46:36


> And that’s where the problem lies. To enable large numbers of enum values,
> I’d have to find a solution without using Boost.PP. Without C++ introducing
> argument number based macro overloading, that is not possible, short of
> defining my own macro sequences for more than 256 elements, which I would like to avoid.
>

I wonder, how many people are out there who want to go beyond 256 :)

Have you considered X-macro trick (say, here:
http://stackoverflow.com/a/650729/1198287)?
That definitely doesn't have those limitations, looks simple enough
(although not as neat as Boost.Preprocessor), and also allows you to
generate many things out of it.

Just now tested it with 500 enum elements:

#include <iostream>

#define MYENUM \
    X(Val1) \
    X(Val2) \
...
    X(Val499) \
    X(Val500)

enum class MyEnum
{
#define X(EnumVal_) EnumVal_,
    MYENUM
#undef X
};

const char* ToString(MyEnum e)
{
    switch (e)
    {
#define X(EnumVal_) case MyEnum::EnumVal_: return #EnumVal_;
    MYENUM
#undef X
default:
return "Unknown!";
    }
}

int main()
{
    std::cout << ToString(MyEnum::Val102) << std::endl;
    std::cout << ToString(MyEnum::Val500) << std::endl;
}

-- 
Best regards,
Artem Tokmakov

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk