C++ Logo

std-proposals

Advanced search

Re: [std-proposals] enum with non-integral types

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 14 Mar 2025 10:58:49 -0700
On Friday, 14 March 2025 09:27:13 Pacific Daylight Time Filip via Std-Proposals
wrote:
> I would like to use enums with different underlying type.
> I have considered using inheritance style list:

Why? Specifically, why does it need to be an enum?

> enum struct Foo : int, float {
> A {3, 0.f},
> B { …, 1.f} // `…` to indicate that following element should follow
> increment from previous value>
> };

struct Foo { int i; float f; };
constexpr Foo
    A = {3, 0.f},
    B = {4, 1.f};

What benefit does having it an enum provide that a literal type won't, side
from a slight shorter syntax with maybe an auto-increment (which may not be
allowed for all member types anyway)?

I can only see drawbacks. For example, it neither converts to an integer nor a
floating point. Maybe you could perform a structured binding to get the values,
but why not have proper names for them?

> However it does pose several problems so other possibility is to use only
> one type and an initializer list. Type used should have:
> + constexpr operator ++ const [if the values are not provided, could be
> explicitly deleted to force user to implement all values]

Incrementing only one element or all of them? For floating point types, should
it increment by one, by one ulp, or by some other scaled value? Or maybe it
should just double the previous value.


-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2025-03-14 17:58:51