From: Simon Marchi <simon.marchi@polymtl.ca>
To: Pedro Alves <palves@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 2/3] enum_flags: Fix problems and add comprehensive unit tests
Date: Sat, 05 Nov 2016 02:47:00 -0000 [thread overview]
Message-ID: <213333898f243221ef2d584fba50daf7@polymtl.ca> (raw)
In-Reply-To: <1478229738-24469-3-git-send-email-palves@redhat.com>
On 2016-11-03 23:22, Pedro Alves wrote:
> This patch starts by adding comprehensive unit tests for enum_flags.
>
> It adds:
>
> - tests of normal expected uses of the API.
>
> - checks that _invalid_ uses of the API would fail to compile. I.e.,
> it validates that enum_flags really is a strong type, and that
> incorrect mixing of enum types would be caught at compile time. It
> pulls that off making use of SFINEA and C++11's decltype/constexpr.
>
> This revealed many holes in the enum_flags API. For example, the f1
> assignment below currently incorrectly fails to compile:
>
> enum_flags<flags> f1 = FLAG1;
> enum_flags<flags> f2 = FLAG2 | f1;
>
> This hole and more are all plugged by this patch, by reworking how the
> enum_flags operators are implemented, and making use of C++11's
> feature of being able to delete methods/functions.
>
> This makes most of the enum_flags operators constexpr. Beyond
> enabling more compiler optimizations and enabling the new unit tests,
> this has other advantages, like making it possible to use operator|
> with enum_flags values in switch cases, where only compile-time
> constants are allowed:
>
> enum_flags<flags> f = FLAG1 | FLAG2;
> switch (f)
> {
> case FLAG1 | FLAG2:
> break;
> }
>
> Currently that fails to compile.
>
> This adds a STATIC_SELF_TEST macro to selftest.h, which is a variant
> of SELF_TEST, but uses C++11's static_assert to do checking at compile
> time.
>
> To avoid potential ODR violations, since the tests add enums with
> short names that could easily conflict with other names, the new
> selftests are placed in a namespace (selftests::enum_flags_tests). I
> think that's a good practice that we should start following. This
> required splitting the global operator overload enablement out of the
> DEF_ENUM_FLAGS_TYPE macro into a separate macro, because
> DEF_ENUM_FLAGS_TYPE wants to create the enum flags typedef in the
> global namespace too.
>
> Tested with gcc 4.8, 4.9, 5.3, 7 (trunk) and clang 3.7.
Yay, more unit tests!
I don't understand much about the internal implementation, but as long
as it makes the class easy to use correctly and hard to use incorrectly,
I think it's a win.
As a side-note, there's still the C implementation at the bottom of this
file, it can probably go away.
Thanks,
Simon
next prev parent reply other threads:[~2016-11-05 2:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-11-04 3:23 [PATCH 0/3] " Pedro Alves
2016-11-04 3:23 ` [PATCH 1/3] enum_flags: Use C++11 std::underlying_type Pedro Alves
2016-11-05 2:28 ` Simon Marchi
2016-11-04 3:23 ` [PATCH 3/3] enum_flags: Fix ternary operator and remove implicit convertion to raw enum Pedro Alves
2016-11-04 3:23 ` [PATCH 2/3] enum_flags: Fix problems and add comprehensive unit tests Pedro Alves
2016-11-05 2:47 ` Simon Marchi [this message]
2016-11-05 3:05 ` Simon Marchi
2016-11-07 13:33 ` [PATCH 0/3] " Luis Machado
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=213333898f243221ef2d584fba50daf7@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
--cc=palves@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox