Topic: function based enums


Author: "dgutson ." <danielgutson@gmail.com>
Date: Thu, 11 Feb 2016 03:58:59 -0300
Raw View
--089e01184e62c881f9052b7915e9
Content-Type: text/plain; charset=UTF-8

I am toying with the idea of providing a function (or lambda) to an enum in
order to automatically calculate the enumerators.
That could be specified by writing the function name (or lambda expr) in
place of the enum class underlying type:

enum class E : [] ( size_t i) { return i ;}
{ A, B, C};

The actual underlying type would be the return type of the function.
In case a function is provided, it should be constexpr.
The argument is the ordinal position of the enumerator being assigned.

This feature could be useful for bit map enumerations and compile time
checks:

enum class PowersOf2: [] (size_t i) { assert (i<32); return 1<<i; }
{ One, Two, Four };

As an interesting side possibility the question wether template enums
definitions could be also useful, where the template arguments can
participate in the function definition :

template<class T>
enum class E: f<T>{...};

Feedback?

   Daniel.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

--089e01184e62c881f9052b7915e9
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<p dir=3D"ltr">I am toying with the idea of providing a function (or lambda=
) to an enum in order to automatically calculate the enumerators.<br>
That could be specified by writing the function name (or lambda expr) in pl=
ace of the enum class underlying type:</p>
<p dir=3D"ltr">enum class E : [] ( size_t i) { return i ;}<br>
{ A, B, C};</p>
<p dir=3D"ltr">The actual underlying type would be the return type of the f=
unction.<br>
In case a function is provided, it should be constexpr.<br>
The argument is the ordinal position of the enumerator being assigned.</p>
<p dir=3D"ltr">This feature could be useful for bit map enumerations and co=
mpile time checks:</p>
<p dir=3D"ltr">enum class PowersOf2: [] (size_t i) { assert (i&lt;32); retu=
rn 1&lt;&lt;i; }<br>
{ One, Two, Four };</p>
<p dir=3D"ltr">As an interesting side possibility the question wether templ=
ate enums definitions could be also useful, where the template arguments ca=
n participate in the function definition :</p>
<p dir=3D"ltr">template&lt;class T&gt;<br>
enum class E: f&lt;T&gt;{...};</p>
<p dir=3D"ltr">Feedback?</p>
<p dir=3D"ltr">=C2=A0=C2=A0 Daniel.</p>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/a/isocpp.org/group=
/std-proposals/">https://groups.google.com/a/isocpp.org/group/std-proposals=
/</a>.<br />

--089e01184e62c881f9052b7915e9--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Thu, 11 Feb 2016 08:59:58 +0100
Raw View
Le 11/02/2016 07:58, dgutson . a =C3=A9crit :
>
> I am toying with the idea of providing a function (or lambda) to an=20
> enum in order to automatically calculate the enumerators.
> That could be specified by writing the function name (or lambda expr)=20
> in place of the enum class underlying type:
>
> enum class E : [] ( size_t i) { return i ;}
> { A, B, C};
>
> The actual underlying type would be the return type of the function.
> In case a function is provided, it should be constexpr.
> The argument is the ordinal position of the enumerator being assigned.
>
I would like to see ordinal enums in C++. IMHO, ordinal enums should=20
support some kind of iteration over the enumerators using the position=20
not its value. An ordinal enum should be isomorphic to 1..N, so that we=20
could move from position to value and from value to position.

I don't know if we need to change the language for this. Have you=20
considered a library solution with reflection.


> This feature could be useful for bit map enumerations and compile time=20
> checks:
>
> enum class PowersOf2: [] (size_t i) { assert (i<32); return 1<<i; }
> { One, Two, Four };
>
We could use also ordinal enums as index or arrays, where the position=20
is the enumerator's index.
>
> As an interesting side possibility the question wether template enums=20
> definitions could be also useful, where the template arguments can=20
> participate in the function definition :
>
> template<class T>
> enum class E: f<T>{...};
>
Do you have a concrete example?
How E would be used? as E<T>?

Vicente

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-propos=
als/.

.


Author: Thiago Macieira <thiago@macieira.org>
Date: Thu, 11 Feb 2016 14:34:57 -0800
Raw View
On quinta-feira, 11 de fevereiro de 2016 08:59:58 PST Vicente J. Botet Escriba
wrote:
> I would like to see ordinal enums in C++. IMHO, ordinal enums should
> support some kind of iteration over the enumerators using the position
> not its value. An ordinal enum should be isomorphic to 1..N, so that we
> could move from position to value and from value to position.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3815.html

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: "dgutson ." <danielgutson@gmail.com>
Date: Thu, 11 Feb 2016 19:59:38 -0300
Raw View
On Thu, Feb 11, 2016 at 4:59 AM, Vicente J. Botet Escriba
<vicente.botet@wanadoo.fr> wrote:
> Le 11/02/2016 07:58, dgutson . a =C3=A9crit :
>>
>>
>> I am toying with the idea of providing a function (or lambda) to an enum
>> in order to automatically calculate the enumerators.
>> That could be specified by writing the function name (or lambda expr) in
>> place of the enum class underlying type:
>>
>> enum class E : [] ( size_t i) { return i ;}
>> { A, B, C};
>>
>> The actual underlying type would be the return type of the function.
>> In case a function is provided, it should be constexpr.
>> The argument is the ordinal position of the enumerator being assigned.
>>
> I would like to see ordinal enums in C++. IMHO, ordinal enums should supp=
ort
> some kind of iteration over the enumerators using the position not its
> value. An ordinal enum should be isomorphic to 1..N, so that we could mov=
e
> from position to value and from value to position.

I leave these items since I think they were addressed in N3815 as
shown by Thiago Macieira.

>
> I don't know if we need to change the language for this. Have you conside=
red
> a library solution with reflection.

No I didn't. I don't think that would solve what I want to solve either.

>
>
>> This feature could be useful for bit map enumerations and compile time
>> checks:
>>
>> enum class PowersOf2: [] (size_t i) { assert (i<32); return 1<<i; }
>> { One, Two, Four };
>>
> We could use also ordinal enums as index or arrays, where the position is
> the enumerator's index.

I'm not sure I understood this suggestion. Could you please elaborate?

>>
>>
>> As an interesting side possibility the question wether template enums
>> definitions could be also useful, where the template arguments can
>> participate in the function definition :
>>
>> template<class T>
>> enum class E: f<T>{...};
>>
> Do you have a concrete example?
> How E would be used? as E<T>?

template <class T>
constexpr T f(size_t i)
{
    return sizeof(T) * i;
}

template <class T>
enum class E : f<T>
{
    First,
    Second,
    Third
};

auto x =3D E<int>::Second;

I don't have a concrete real-life example where this could be useful.

>
> Vicente
>
> --
>
> --- You received this message because you are subscribed to the Google
> Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
> https://groups.google.com/a/isocpp.org/group/std-proposals/.



--=20
Who=E2=80=99s got the sweetest disposition?
One guess, that=E2=80=99s who?
Who=E2=80=99d never, ever start an argument?
Who never shows a bit of temperament?
Who's never wrong but always right?
Who'd never dream of starting a fight?
Who get stuck with all the bad luck?

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-propos=
als/.

.