Skip to content

Commit b178735

Browse files
Fix frozen::make_set for the case of N = 0
Thanks to @cbeck88 for spotting the issue.
1 parent d1fc4ce commit b178735

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

include/frozen/bits/basic_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ namespace frozen {
3333

3434
namespace bits {
3535

36+
// used as a fake argument for frozen::make_set and frozen::make_map in the case of N=0
37+
struct ignored_arg {};
38+
3639
template <class T, std::size_t N>
3740
class cvector {
3841
T data [N] = {}; // zero-initialization for scalar type T, default-initialized otherwise

include/frozen/map.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ template <class Comparator> class CompareKey {
6666
}
6767
};
6868

69-
struct ignored_arg {};
70-
7169
} // namespace impl
7270

7371
template <class Key, class Value, std::size_t N, class Compare = std::less<Key>>
@@ -249,7 +247,7 @@ class map<Key, Value, 0, Compare> {
249247
};
250248

251249
template <typename T, typename U>
252-
constexpr auto make_map(impl::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
250+
constexpr auto make_map(bits::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
253251
return map<T, U, 0>{};
254252
}
255253

include/frozen/set.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ template <class Key, std::size_t N, class Compare = std::less<Key>> class set {
131131
};
132132

133133
template <class Key, class Compare> class set<Key, 0, Compare> {
134-
using container_type = bits::carray<Key, 1>; // just for the type definitions
134+
using container_type = bits::carray<Key, 0>; // just for the type definitions
135135
Compare const compare_;
136136

137137
public:
@@ -194,11 +194,17 @@ template <class Key, class Compare> class set<Key, 0, Compare> {
194194
constexpr const_reverse_iterator crend() const { return nullptr; }
195195
};
196196

197+
template <typename T>
198+
constexpr auto make_set(bits::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
199+
return set<T, 0>{};
200+
}
201+
197202
template <typename T, std::size_t N>
198203
constexpr auto make_set(const T (&args)[N]) {
199204
return set<T, N>(args);
200205
}
201206

207+
202208
} // namespace frozen
203209

204210
#endif

tests/test_set.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,24 @@ TEST_CASE("frozen::set <> frozen::make_set", "[set]") {
235235
for (auto v : frozen_set)
236236
REQUIRE(frozen_set2.count(v));
237237
}
238+
239+
constexpr frozen::set<short, 0> frozen_empty_set = {};
240+
constexpr auto frozen_empty_set2 = frozen::make_set<short>();
241+
constexpr auto frozen_empty_set3 = frozen::make_set<short>({});
242+
243+
SECTION("checking empty set") {
244+
REQUIRE(frozen_empty_set.empty());
245+
REQUIRE(frozen_empty_set.size() == 0);
246+
REQUIRE(frozen_empty_set.begin() == frozen_empty_set.end());
247+
248+
REQUIRE(frozen_empty_set2.empty());
249+
REQUIRE(frozen_empty_set2.size() == 0);
250+
REQUIRE(frozen_empty_set2.begin() == frozen_empty_set2.end());
251+
252+
REQUIRE(frozen_empty_set3.empty());
253+
REQUIRE(frozen_empty_set3.size() == 0);
254+
REQUIRE(frozen_empty_set3.begin() == frozen_empty_set3.end());
255+
}
238256
}
239257

240258
TEST_CASE("frozen::set constexpr", "[set]") {

0 commit comments

Comments
 (0)