Skip to content

Commit

Permalink
Move initializer-list check to basic types
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-sans-paille committed Jan 13, 2025
1 parent 06bee53 commit 72047f9
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 14 deletions.
9 changes: 6 additions & 3 deletions include/frozen/bits/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define FROZEN_LETITGO_BASIC_TYPES_H

#include "frozen/bits/exceptions.h"
#include "frozen/bits/constexpr_assert.h"

#include <array>
#include <utility>
Expand Down Expand Up @@ -96,6 +97,10 @@ class carray {
constexpr carray(const T& value, std::index_sequence<I...>)
: data_{((void)I, value)...} {}

static constexpr void check_initializer(std::initializer_list<T> init) {
constexpr_assert(init.size() >= N, "Cannot initialize a carray with an smaller initializer list");
}

public:
// Container typdefs
using value_type = T;
Expand Down Expand Up @@ -126,10 +131,8 @@ class carray {
}
template <typename U, std::enable_if_t<std::is_convertible<U, T>::value>* = nullptr>
constexpr carray(std::initializer_list<U> init)
: carray(init.begin(), std::make_index_sequence<N>())
: carray((check_initializer(init), init.begin()), std::make_index_sequence<N>())
{
// clang & gcc doesn't recognize init.size() as a constexpr
// static_assert(init.size() >= N, "Cannot initialize a carray with an smaller initializer list");
}
template <typename U, std::enable_if_t<std::is_convertible<U, T>::value>* = nullptr>
constexpr carray(const carray<U, N>& rhs)
Expand Down
3 changes: 1 addition & 2 deletions include/frozen/bits/constexpr_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@

// FIXME: find a way to implement that correctly for msvc
#define constexpr_assert(cond, msg)

#else

#define constexpr_assert(cond, msg)\
assert(cond && msg);
assert(cond && msg)
#endif

#endif
Expand Down
2 changes: 0 additions & 2 deletions include/frozen/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "frozen/bits/algorithms.h"
#include "frozen/bits/basic_types.h"
#include "frozen/bits/constexpr_assert.h"
#include "frozen/bits/exceptions.h"
#include "frozen/bits/mpl.h"
#include "frozen/bits/version.h"
Expand Down Expand Up @@ -105,7 +104,6 @@ class map : private impl::CompareKey<Compare> {

constexpr map(std::initializer_list<value_type> items, Compare const &compare)
: map{container_type {items}, compare} {
constexpr_assert(items.size() == N, "Inconsistent initializer_list size and type size argument");
}

constexpr map(std::initializer_list<value_type> items)
Expand Down
2 changes: 0 additions & 2 deletions include/frozen/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "frozen/bits/algorithms.h"
#include "frozen/bits/basic_types.h"
#include "frozen/bits/constexpr_assert.h"
#include "frozen/bits/version.h"
#include "frozen/bits/defines.h"

Expand Down Expand Up @@ -69,7 +68,6 @@ template <class Key, std::size_t N, class Compare = std::less<Key>> class set :

constexpr set(std::initializer_list<Key> keys, Compare const & comp)
: set{container_type{keys}, comp} {
constexpr_assert(keys.size() == N, "Inconsistent initializer_list size and type size argument");
}

constexpr set(std::initializer_list<Key> keys)
Expand Down
5 changes: 2 additions & 3 deletions include/frozen/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#define FROZEN_LETITGO_UNORDERED_MAP_H

#include "frozen/bits/basic_types.h"
#include "frozen/bits/constexpr_assert.h"
#include "frozen/bits/elsa.h"
#include "frozen/bits/exceptions.h"
#include "frozen/bits/pmh.h"
Expand Down Expand Up @@ -86,12 +85,12 @@ class unordered_map : private KeyEqual {
bits::make_pmh_tables<storage_size>(
items_, hash, bits::GetKey{}, default_prg_t{})} {}
explicit constexpr unordered_map(container_type items)
: unordered_map{items, Hash{}, KeyEqual{}} {}
: unordered_map{items, Hash{}, KeyEqual{}} {
}

constexpr unordered_map(std::initializer_list<value_type> items,
Hash const & hash, KeyEqual const & equal)
: unordered_map{container_type{items}, hash, equal} {
constexpr_assert(items.size() == N, "Inconsistent initializer_list size and type size argument");
}

constexpr unordered_map(std::initializer_list<value_type> items)
Expand Down
2 changes: 0 additions & 2 deletions include/frozen/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#define FROZEN_LETITGO_UNORDERED_SET_H

#include "frozen/bits/basic_types.h"
#include "frozen/bits/constexpr_assert.h"
#include "frozen/bits/elsa.h"
#include "frozen/bits/pmh.h"
#include "frozen/bits/version.h"
Expand Down Expand Up @@ -87,7 +86,6 @@ class unordered_set : private KeyEqual {

constexpr unordered_set(std::initializer_list<Key> keys, Hash const & hash, KeyEqual const & equal)
: unordered_set{container_type{keys}, hash, equal} {
constexpr_assert(keys.size() == N, "Inconsistent initializer_list size and type size argument");
}

/* iterators */
Expand Down

0 comments on commit 72047f9

Please sign in to comment.