Subject: Re: [boost] [Better Enums] More complete release - feedback requested
From: Anton Bachin (antonbachin_at_[hidden])
Date: 2015-06-11 11:06:56


Thanks for the reply :)

> On Jun 11, 2015, at 09:46, Thorsten Ottosen <tottosen_at_[hidden]> wrote:
>
> On 11-06-2015 15:47, Anton Bachin wrote:
>
>>>> [snip]
>>>>
>>>>> B. make size a constexpr function
>>>
>>>
>>> +1
>>>
>>> I think you should look to the future and give pay-off to people using more
>>> modern C++. However you can macro-ize the constexpr keyword so that it is
>>> constexpr is supported or nothing if not.
>>>
>>> BOOST_NO_CXX11_CONSTEXPR will do the check for you.
>>
>> I’ve already done this, and I use it throughout enum.h. It’s not a problem to
>> make a size() function constexpr only when supported, I am just not so
>> comfortable throwing away size as an integral constant for non-C++11 users.
>> Perhaps I can make size() a function and provide an alternative constant with an
>> ugly name for those who might need it? Do people still use C++03? And, again,
>> what is the rationale for size being a function?
>
> It's more generic if you want to view the class as a container with size() and begin()/end().

Better Enums is not the container, however. It’s a class that provides access to
two containers, currently through _names() and _values(). Those containers do
have size() (a function), and begin()/end():

http://aantron.github.io/better-enums/ApiReference.html#Typedef_value_iterable
http://aantron.github.io/better-enums/ApiReference.html#Typedef_name_iterable

These are containers in the sense you mean, compatible with STL algorithms,
for-each loops, etc. I do value having size be a function for uniformity, so
that it’s not surprising that it’s a function in the containers, yet a constant
in the enum. I’m leaning towards your suggestion, and providing a constant with
an alternative name for C++03 metaprogramming.

> Yes, people still used C++03, but I'm sure they can live with a non-constexpr size() that is every bit as fast at runtime as a constexpr one.

Well, the issue isn’t runtime performance, it’s that you can’t use the function
for some purposes before C++11. For example, only the first one works:

    const int size = 3;
    char *descriptions[size];

    int size() { return 3; }
    char *descriptions[size()];

I may be somehow wrong with this usage example. If so, please correct me.

Regards,
Anton