Le 03/02/2016 10:01, Niall Douglas a écrit :
Dear Boost,
As part of the AFIO v2 rewrite I've been trying to improve on how AFIO v1 did bitfields by making better use of constexpr and the type system to make usage more natural. I'd appreciate feedback on my efforts.
AFIO v1 has an example of usage at https://goo.gl/oVLIjU or https://gist.github.com/ned14/9ef2a9d00da0ffc877b8 and it took the following form:
enum class flag : size_t { none=0, delete_on_close=1, disable_safety_fsyncs=2 }; BOOST_AFIO_DECLARE_CLASS_ENUM_AS_BITFIELD(flag)
The BOOST_AFIO_DECLARE_CLASS_ENUM_AS_BITFIELD macro declares all the sensible global overloads you'd expect, is type safe and you can use it without surprise except in one single instance: testing for emptiness. The problem is that you cannot declare as a global operator an explicit operator bool nor member functions on enums, so one cannot do if(f) though if(!f) is fine. In AFIO v1, I used if(!!f) and if(!!(f & flag::foo)) everywhere, which is fine if a little abtuse. TBoost.Enums [1] has an alternative design, given an ordinal enum E we can create a enum_set<E> [2] that behaves like a bitset<N>, but we can use the enumerators instead of the position. I'm not saying it is better or worst, I'm just giving the pointer here in case this can help someone.
AFIO v2's current bitfield has an example of usage at https://goo.gl/LsjSGD or https://gist.github.com/ned14/89ee39c6b8eb5254116a and it takes the following form:
struct flag : bitwise_flags<flag> { flag() = default; constexpr flag(bitwise_flags<flag> v) noexcept : bitwise_flags<flag>(v) { } static constexpr auto none(){ return bit(0);} static constexpr auto delete_on_close(){ return bit(1); } static constexpr auto disable_safety_fsyncs(){ return bit(2); } };
What is the bit above? what is the decltype of bit(0)? Could flag store bit(5)?
So, do Boosters think we can actually make a C++ 14 bitfield which:
1. Is typesafe, so not a C bitfield.
2. Is convenient for programmers to declare (i.e. little boilerplate in a specific bitfield declaration).
3. Works as you'd expect a bitfield to work, including bitfield::flag.
4. if(bf) works.
5. Is 100% constexpr and generates zero runtime overhead.
Yes, I believe we need something like that. Vicente [1] https://htmlpreview.github.io/?https://github.com/viboes/enums/blob/master/l... [2] https://htmlpreview.github.io/?https://github.com/viboes/enums/blob/master/l... P.S. This library is an old one that needs to be refactored to take advantage of C++14 and is not ready for review :(.