Skip to content

Commit 17c8c50

Browse files
committed
Replace static_const with inline constexpr variables (#66)
1 parent 3bd66ea commit 17c8c50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+111
-477
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ in [Boost.Sort](https://www.boost.org/doc/libs/1_80_0/libs/sort/doc/html/index.h
171171
by Francisco Jose Tapia.
172172

173173
* [`utility::as_function`](https://github.com/Morwenn/cpp-sort/wiki/Miscellaneous-utilities#as_function),
174-
[`utility::static_const`](https://github.com/Morwenn/cpp-sort/wiki/Miscellaneous-utilities#static_const),
175174
and several projection-enhanced helper algorithms come from Eric Niebler's [Range
176175
v3](https://github.com/ericniebler/range-v3) library. Several ideas such as proxy
177176
iterators, customization points and projections, as well as a few other utility

docs/Miscellaneous-utilities.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -531,34 +531,12 @@ auto swap_index_pairs_force_unroll(RandomAccessIterator first,
531531
532532
*New in version 1.11.0*
533533
534-
### `static_const`
535-
536-
```cpp
537-
#include <cpp-sort/utility/static_const.h>
538-
```
539-
540-
***WARNING:** `utility::static_const` is removed in version 2.0.0, use [`inline` variables][inline-variables] instead.*
541-
542-
`static_const` is a tiny utility used to instantiate function objects (for example sorters) and expose a single global instance to users of the library while avoiding ODR problems. It is taken straight from [Range-v3][range-v3]. The general pattern to instantiate function objects is as follows:
543-
544-
```cpp
545-
namespace
546-
{
547-
constexpr auto&& awesome_sort
548-
= cppsort::utility::static_const<awesome_sorter>::value;
549-
}
550-
```
551-
552-
You can read more about this instantiation pattern in [this article][eric-niebler-static-const] by Eric Niebler.
553-
554534
555535
[apply-permutation]: Miscellaneous-utilities.md#apply_permutation
556536
[chainable-projections]: Chainable-projections.md
557537
[callable]: https://en.cppreference.com/w/cpp/named_req/Callable
558538
[ebo]: https://en.cppreference.com/w/cpp/language/ebo
559-
[eric-niebler-static-const]: https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/
560539
[fixed-size-sorters]: Fixed-size-sorters.md
561-
[inline-variables]: https://en.cppreference.com/w/cpp/language/inline
562540
[is-stable]: Sorter-traits.md#is_stable
563541
[metrics]: Metrics.md
564542
[numpy-argsort]: https://numpy.org/doc/stable/reference/generated/numpy.argsort.html

docs/Writing-a-bubble_sorter.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,9 @@ We use forwarding references to ensure that the range overload works with lvalue
393393
The sorter abstraction is useful, but most of the time we only need a sorting algorithm. Therefore, it might be a good idea to instantiate `bubble_sorter` and to have a global `bubble_sort` instance, more versatile than the original `bubble_sort` algorithm.
394394

395395
```cpp
396-
// C++14
397-
namespace
398-
{
399-
constexpr auto&& bubble_sort
400-
= cppsort::utility::static_const<bubble_sorter>::value;
401-
}
402-
403-
// C++17
404396
inline constexpr bubble_sorter bubble_sort{};
405397
```
406398

407-
The combination of [`utility::static_const`][utility-static-const] with an anonymous namespace is a trick used to avoid ODR problems; you can read more about how and why it works in [Eric Niebler's original article](https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/). It is basically a poor man's substitute to compensate the lack of `inline` variables pre-C++17.
408-
409399
## Better error messages
410400

411401
We now have a versatile `bubble_sorter`, able to handle many scenarios, and optimized as much as a bubble sort can be without turning it into a different algorithm. It works really well... until it doesn't. **cpp-sort** has one major drawback there: when not used correctly, the error messages are often close to unreadable; forget one `const` and embrace the hundreds of lines of cryptic SFINAE error messages, and I really mean it!. The sorter works properly, but we can still somewhat improve the way it fails.
@@ -459,5 +449,4 @@ That's it: we have covered pretty much every interesting aspect of writing a sim
459449
[utility-identity]: Miscellaneous-utilities.md#miscellaneous-function-objects
460450
[utility-iter-move]: Miscellaneous-utilities.md#iter_move-and-iter_swap
461451
[utility-size]: Miscellaneous-utilities.md#size
462-
[utility-static-const]: Miscellaneous-utilities.md#static_const
463452
[writing-a-sorter]: Writing-a-sorter.md

docs/Writing-a-sorter.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Now, let's define a set of rules to apply when writing sorters. These rules don'
6363

6464
**Rule 1.4:** *sorters* shall be immutable and every overload of `operator()` shall explicitly be marked `const` (make sure to check twice: forgetting to `const`-qualify them can cause hundreds of lines of cryptic error messages). Some parts of the library *may* accept mutable sorters, but that's never guaranteed unless specified otherwise.
6565

66-
**Rule 1.5:** *sorter* implementers are encouraged but not required to provide a default instance of their *sorters* for convenience. `inline` variables (C++17) or the library's [`static_const`][static-const] utility (C++14) can be used to avoid ODR-related problems.
66+
**Rule 1.5:** *sorter* implementers are encouraged but not required to provide a default instance of their *sorters* for convenience. `inline` variables can be used to avoid ODR-related problems.
6767

6868
## Category of iterators
6969

@@ -579,7 +579,6 @@ In the example above, the resulting sorter will use our `low_projections_sorter`
579579
[sqrtsort]: https://github.com/Mrrl/SqrtSort
580580
[stability]: https://en.wikipedia.org/wiki/Sorting_algorithm#Stability
581581
[stable-adapter]: Sorter-adapters.md#stable_adapter-make_stable-and-stable_t
582-
[static-const]: Miscellaneous-utilities.md#static_const
583582
[std-array]: https://en.cppreference.com/w/cpp/container/array
584583
[std-greater-void]: https://en.cppreference.com/w/cpp/utility/functional/greater_void
585584
[std-identity]: https://en.cppreference.com/w/cpp/utility/functional/identity

examples/bubble_sorter.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2021 Morwenn
2+
* Copyright (c) 2015-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#include <cstddef>
@@ -11,7 +11,6 @@
1111
#include <cpp-sort/utility/as_function.h>
1212
#include <cpp-sort/utility/iter_move.h>
1313
#include <cpp-sort/utility/size.h>
14-
#include <cpp-sort/utility/static_const.h>
1514

1615
namespace detail
1716
{
@@ -95,11 +94,8 @@ struct bubble_sorter:
9594
cppsort::sorter_facade<detail::bubble_sorter_impl>
9695
{};
9796

98-
namespace
99-
{
100-
constexpr auto&& bubble_sort
101-
= cppsort::utility::static_const<bubble_sorter>::value;
102-
}
97+
inline constexpr bubble_sorter bubble_sort{};
98+
10399

104100
#include <algorithm>
105101
#include <array>

include/cpp-sort/comparators/case_insensitive_less.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <locale>
1414
#include <type_traits>
1515
#include <utility>
16-
#include <cpp-sort/utility/static_const.h>
1716
#include "../detail/type_traits.h"
1817

1918
namespace cppsort
@@ -271,12 +270,7 @@ namespace cppsort
271270

272271
using case_insensitive_less_t = detail::case_insensitive_less_fn;
273272

274-
namespace
275-
{
276-
constexpr auto&& case_insensitive_less = utility::static_const<
277-
detail::case_insensitive_less_fn
278-
>::value;
279-
}
273+
inline constexpr detail::case_insensitive_less_fn case_insensitive_less{};
280274
}
281275

282276
#endif // CPPSORT_COMPARATORS_CASE_INSENSITIVE_LESS_H_

include/cpp-sort/comparators/natural_less.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2019 Morwenn
2+
* Copyright (c) 2016-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_COMPARATORS_NATURAL_LESS_H_
@@ -11,7 +11,6 @@
1111
#include <cctype>
1212
#include <iterator>
1313
#include <utility>
14-
#include <cpp-sort/utility/static_const.h>
1514

1615
namespace cppsort
1716
{
@@ -113,12 +112,7 @@ namespace cppsort
113112

114113
using natural_less_t = detail::natural_less_fn;
115114

116-
namespace
117-
{
118-
constexpr auto&& natural_less = utility::static_const<
119-
detail::natural_less_fn
120-
>::value;
121-
}
115+
inline constexpr detail::natural_less_fn natural_less{};
122116
}
123117

124118
#endif // CPPSORT_COMPARATORS_NATURAL_LESS_H_

include/cpp-sort/comparators/partial_greater.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2021 Morwenn
2+
* Copyright (c) 2016-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_COMPARATORS_PARTIAL_GREATER_H_
@@ -12,7 +12,6 @@
1212
#include <utility>
1313
#include <cpp-sort/comparators/weak_greater.h>
1414
#include <cpp-sort/utility/branchless_traits.h>
15-
#include <cpp-sort/utility/static_const.h>
1615
#include "../detail/type_traits.h"
1716

1817
namespace cppsort
@@ -59,12 +58,7 @@ namespace cppsort
5958

6059
using partial_greater_t = detail::partial_greater_fn;
6160

62-
namespace
63-
{
64-
constexpr auto&& partial_greater = utility::static_const<
65-
detail::partial_greater_fn
66-
>::value;
67-
}
61+
inline constexpr detail::partial_greater_fn partial_greater{};
6862

6963
// Branchless traits
7064

include/cpp-sort/comparators/partial_less.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2021 Morwenn
2+
* Copyright (c) 2016-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_COMPARATORS_PARTIAL_LESS_H_
@@ -12,7 +12,6 @@
1212
#include <utility>
1313
#include <cpp-sort/comparators/weak_less.h>
1414
#include <cpp-sort/utility/branchless_traits.h>
15-
#include <cpp-sort/utility/static_const.h>
1615
#include "../detail/type_traits.h"
1716

1817
namespace cppsort
@@ -59,12 +58,7 @@ namespace cppsort
5958

6059
using partial_less_t = detail::partial_less_fn;
6160

62-
namespace
63-
{
64-
constexpr auto&& partial_less = utility::static_const<
65-
detail::partial_less_fn
66-
>::value;
67-
}
61+
inline constexpr detail::partial_less_fn partial_less{};
6862

6963
// Branchless traits
7064

include/cpp-sort/comparators/total_greater.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2022 Morwenn
2+
* Copyright (c) 2016-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_COMPARATORS_TOTAL_GREATER_H_
@@ -12,7 +12,6 @@
1212
#include <type_traits>
1313
#include <utility>
1414
#include <cpp-sort/utility/branchless_traits.h>
15-
#include <cpp-sort/utility/static_const.h>
1615
#include "../detail/floating_point_weight.h"
1716
#include "../detail/type_traits.h"
1817

@@ -68,12 +67,7 @@ namespace cppsort
6867

6968
using total_greater_t = detail::total_greater_fn;
7069

71-
namespace
72-
{
73-
constexpr auto&& total_greater = utility::static_const<
74-
detail::total_greater_fn
75-
>::value;
76-
}
70+
inline constexpr detail::total_greater_fn total_greater{};
7771

7872
// Branchless traits
7973

0 commit comments

Comments
 (0)