Date: Wed, 12 Mar 2025 10:31:33 +0100
Would the actual value, e.g.
- the const char array "three", or
- a pointer to a static duration "three" (uniqueness of pointer between different TU???)
be stored inside the enum?
Or would there be some runtime / (compile-time??? compile-time parameters only?) translation between "three" and the stored 3?
Perhaps you are looking for some higher-level enum (performance and memory footprint not critical; safety and ease-of-use more relevant). Some mixture of std::enum and a checked std::variant?
-----Ursprüngliche Nachricht-----
Von:Filip via Std-Proposals <std-proposals_at_[hidden]>
Gesendet:Mi 12.03.2025 10:13
Betreff:Re: [std-proposals] lambdas in enums
An:std-proposals_at_[hidden];
CC:Filip <fph2137_at_[hidden]>; std-proposals_at_[hidden];
What about having enum with alternate types?
enum struct{
a = 0,
b<int> = 3,
b<const char*> = “three”
};
Or
enum struct {
a = 0,
b = 3 | “three”
};
The user can use static_cast to change from one to the other.
In my cases something like this would eliminate functions and make it clearer what enums are taken care of.
Cheers, Filip
Wiadomość napisana przez Robin Savonen Söderholm via Std-Proposals <std-proposals_at_[hidden]> w dniu 12 mar 2025, o godz. 07:39:
I paraphrase Mateusz Pusz from one of his courses: prefer free functions over member functions.
ADL is a thing in C++ and I see no benefits of this proposal vs to utilize ADL and just define a free "to_string"-method in the enclosing namespace. Free functions found with ADL are just as much part of a types API as their member functions.
// Robin
On Wed, Mar 12, 2025, 06:47 Simon Schröder via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote:
> On Mar 12, 2025, at 2:23 AM, Dmitrii Shabalin via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote:
>
> If you have any idea how to have a function inside in the enum, please, share your idea with an example.
My first thought was that instead of allowing enums to have member functions I would put an enum inside a class:
class Test
{
public:
// not en enum class on purpose!
enum TestEnum { test_value = 0 };
static string_view to_string(TestEnum);
}
Declaring a variable is slightly more to type:
Test::TestEnum e = test_value;
and you can‘t call it as a member function:
e.to_string(); // not possible
However, the “member function call” could be solved differently. We could, once more, try to allow for member call syntax to also look for “free standing” (or otherwise scoped functions) that take the type as the first parameter. (With this solution it doesn’t really matter if we put the enum inside a namespace or class.)
--
Std-Proposals mailing list
Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
--
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-03-12 09:36:40