Skip to content

Commit

Permalink
Add __may_alias__ attribute to gsl::byte (#668)
Browse files Browse the repository at this point in the history
C++17 defines `std::byte*` to have the same aliasing properties as
`char*`. Hence, we mark it with the `__may_alias__` attribute under gcc
and clang.

Based on the fix by Martin Moene in byte-lite.

Fixes #663
  • Loading branch information
StephanDollberg authored and Anna Gringauze committed May 1, 2018
1 parent c4c1f63 commit d6b26b3
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/gsl/gsl_byte
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@

#endif // _MSC_VER

// Use __may_alias__ attribute on gcc and clang
#if defined __clang__ || (__GNUC__ > 5)
#define byte_may_alias __attribute__((__may_alias__))
#else // defined __clang__ || defined __GNUC__
#define byte_may_alias
#endif // defined __clang__ || defined __GNUC__

namespace gsl
{
#if GSL_USE_STD_BYTE
Expand All @@ -69,7 +76,7 @@ using std::to_integer;

// This is a simple definition for now that allows
// use of byte within span<> to be standards-compliant
enum class byte : unsigned char
enum class byte_may_alias byte : unsigned char
{
};

Expand Down

1 comment on commit d6b26b3

@steakhal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this byte_may_alias attribute solve the aliasing problems with gsl::byte?
I mean in the README.md still states that:

For gsl::byte to work correctly with Clang and GCC you might have to use the -fno-strict-aliasing compiler option.

Which is quite old - according to the git-blame its about 3 years old now.
The question is: Should we update the README.md or the attribute did not solve the issue completely?

Please sign in to comment.