Skip to content

Commit 3318bf9

Browse files
Ghabrymgambrell
andcommitted
Make enum tags compile on older clang compilers
std::array ist not constexpr Co-Authored-By: mgambrell <mgambrell@users.noreply.github.com>
1 parent f603467 commit 3318bf9

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/lcf/enum_tags.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <type_traits>
1414
#include <iterator>
1515
#include <cstddef>
16-
#include <array>
1716
#include <cstring>
1817
#include <tuple>
1918

@@ -68,12 +67,10 @@ class EnumTags {
6867
bool etag(const char* tag, E& result) const;
6968
E etagOr(const char* tag, E other) const;
7069

71-
const std::array<EnumItem, num_tags>& tags() const { return _tags; }
72-
7370
constexpr bool is_monotonic_from_zero() const { return monotonic_from_zero; }
7471

75-
constexpr iterator begin() const { return iterator(_tags.data()); }
76-
constexpr iterator end() const { return iterator(_tags.data() + size()); }
72+
constexpr iterator begin() const { return iterator(std::data(_tags)); }
73+
constexpr iterator end() const { return iterator(std::data(_tags) + size()); }
7774

7875
constexpr iterator cbegin() const { return begin(); }
7976
constexpr iterator cend() const { return end(); }
@@ -102,7 +99,8 @@ class EnumTags {
10299
}
103100
}
104101

105-
std::array<EnumItem, num_tags> _tags;
102+
// std::array is not constexpr on some older C++17 compilers
103+
EnumItem _tags[num_tags]{};
106104
bool monotonic_from_zero = true;
107105
};
108106

@@ -150,7 +148,7 @@ inline constexpr const char* EnumTags<E, N>::operator[](int_type value) const {
150148

151149
template <typename E, size_t N>
152150
inline bool EnumTags<E, N>::has_etag(const char* tag) const {
153-
for (size_t i = 0; i < _tags.size(); ++i) {
151+
for (size_t i = 0; i < std::size(_tags); ++i) {
154152
if (std::strcmp(_tags[i].name, tag) == 0) {
155153
return true;
156154
}
@@ -161,7 +159,7 @@ inline bool EnumTags<E, N>::has_etag(const char* tag) const {
161159

162160
template <typename E, size_t N>
163161
inline bool EnumTags<E, N>::etag(const char* tag, E& result) const {
164-
for (size_t i = 0; i < _tags.size(); ++i) {
162+
for (size_t i = 0; i < std::size(_tags); ++i) {
165163
if (std::strcmp(_tags[i].name, tag) == 0) {
166164
result = E(_tags[i].value);
167165
return true;
@@ -173,7 +171,7 @@ inline bool EnumTags<E, N>::etag(const char* tag, E& result) const {
173171

174172
template <typename E, size_t N>
175173
inline E EnumTags<E, N>::etagOr(const char* tag, E other) const {
176-
for (size_t i = 0; i < _tags.size(); ++i) {
174+
for (size_t i = 0; i < std::size(_tags); ++i) {
177175
if (std::strcmp(_tags[i].name, tag) == 0) {
178176
return E(_tags[i].value);
179177
}

0 commit comments

Comments
 (0)