Skip to content

Commit

Permalink
range size() is constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
rollbear committed Feb 22, 2024
1 parent dec4909 commit 33f3af8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/strong_type/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <typename T, typename Tag, typename ... M>
class range::modifier<
type<T, Tag, M...>,
impl::void_t<
decltype(std::declval<T>().begin() == std::declval<T>().end())
decltype(std::declval<T&>().begin() == std::declval<T&>().end())
>
>
{
Expand Down Expand Up @@ -97,6 +97,7 @@ class range::modifier<
}

template <typename TT = const T&>
constexpr
decltype(std::declval<TT>().size())
size()
const
Expand Down
16 changes: 16 additions & 0 deletions test/test_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "catch2.hpp"

#include <forward_list>
#include <array>

namespace {
template<typename R, typename = int>
Expand Down Expand Up @@ -80,4 +81,19 @@ TEST_CASE("range of sized type has size")

iv v{3,2,1, 0};
REQUIRE(v.size() == 4);
}

TEST_CASE("constexpr size")
{
using ia = strong::type
<
std::array<int, 4>,
struct ia_,
strong::range,
strong::default_constructible
>;

static constexpr ia a{};
REQUIRE(a.size() == 4);
STATIC_REQUIRE(a.size() == 4);
}

0 comments on commit 33f3af8

Please sign in to comment.