Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid hardcoding enum type

Tags:

c++

c++11

In c++11 code it would be nice to avoid mentioning a specific enum qualifier every time I use the enum value - as it is a new code and it is refactored a lot.

For that purpose is it possible something in the spirit of the last line of this pseudo code:

enum abc { a,b,c };
// some long code of events which returns the enum's value
auto e = []()->abc{return abc::b;}();
if (e == std::declval(e)::a) { ...

If not possible in C++11 will it become possible in C++14 or 17?

like image 407
Vlad Didenko Avatar asked Nov 11 '16 20:11

Vlad Didenko


1 Answers

You're close, you can use decltype:

if (e == decltype(e)::a) {
    ...
like image 166
krzaq Avatar answered Nov 07 '22 19:11

krzaq