C++ Logo

std-proposals

Advanced search

[std-proposals] lambdas in enums

From: Dmitrii Shabalin <ckpyt.new_at_[hidden]>
Date: Wed, 12 Mar 2025 12:20:34 +1300
Hi.
Since C++11 we have lambdas.
I believe, everyone, who works with enums, write some functions for this
particular enum only.
However, there is no way to associate this function with the enum's
namespace.
My idea: we can set a value for any of enum fields. It should be const and
could be constexpr.
May be, we can set a lambda for a field too?
something like:
enum Test{
testValue = 0,
toString =[](Test value) -> std::string { if (value == testValue) return
"testValue"; else return ""; },
max = testValue
}

And usage will be something like:
std::cout << "Test value" << Test::toString(Test::testValue);

Received on 2025-03-11 23:20:48