sieve constructor should not allocate
This commit is contained in:
parent
3c091551f5
commit
115ad715d5
1 changed files with 7 additions and 5 deletions
|
|
@ -12,7 +12,7 @@ template <typename Derived, size_t Limit>
|
||||||
requires(Limit > 0)
|
requires(Limit > 0)
|
||||||
class Sieve {
|
class Sieve {
|
||||||
public:
|
public:
|
||||||
constexpr explicit Sieve() { static_cast<Derived *>(this)->build(); }
|
constexpr explicit Sieve() noexcept { static_cast<Derived *>(this)->build(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::bitset<Limit + 1> sieve;
|
std::bitset<Limit + 1> sieve;
|
||||||
|
|
@ -41,13 +41,15 @@ class Eratosthenes : public Sieve<Eratosthenes<Limit>, Limit> {
|
||||||
[[nodiscard]] constexpr bool operator[](size_t const number) const {
|
[[nodiscard]] constexpr bool operator[](size_t const number) const {
|
||||||
if consteval {
|
if consteval {
|
||||||
if (number > Limit) {
|
if (number > Limit) {
|
||||||
throw std::out_of_range(std::format(
|
throw std::out_of_range(
|
||||||
"cannot determine primality of {} > LIMIT={}", number, Limit));
|
std::format("cannot determine primality of {} > size of sieve {}",
|
||||||
|
number, Limit));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (number > Limit) {
|
if (number > Limit) {
|
||||||
throw std::out_of_range(std::format(
|
throw std::out_of_range(
|
||||||
"cannot determine primality of {} > LIMIT={}", number, Limit));
|
std::format("cannot determine primality of {} > size of sieve {}",
|
||||||
|
number, Limit));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue