Files
2025-10-25 03:02:53 +03:00

19 KiB

[const.wrap.class]

21 Metaprogramming library [meta]

21.3 Metaprogramming and type traits [type.traits]

21.3.5 Class template constant_wrapper [const.wrap.class]

templatestruct cw-fixed-value { // exposition onlyusing type = T; // exposition onlyconstexpr cw-fixed-value(type v) noexcept : data(v) {} T data; // exposition only};

template<class T, size_t Extent>struct cw-fixed-value<T[Extent]> { // exposition onlyusing type = T[Extent]; // exposition onlyconstexpr cw-fixed-value(T (&arr)[Extent]) noexcept; T data[Extent]; // exposition only};

template<class T, size_t Extent>cw-fixed-value(T (&)[Extent]) -> cw-fixed-value<T[Extent]>; // exposition onlystruct cw-operators { // exposition only// unary operatorstemplate<constexpr-param T>friend constexpr auto operator+(T) noexcept -> constant_wrapper<(+T::value)>{ return {}; }template<constexpr-param T>friend constexpr auto operator-(T) noexcept -> constant_wrapper<(-T::value)>{ return {}; }template<constexpr-param T>friend constexpr auto operator~(T) noexcept -> constant_wrapper<(~T::value)>{ return {}; }template<constexpr-param T>friend constexpr auto operator!(T) noexcept -> constant_wrapper<(!T::value)>{ return {}; }template<constexpr-param T>friend constexpr auto operator&(T) noexcept -> constant_wrapper<(&T::value)>{ return {}; }template<constexpr-param T>friend constexpr auto operator*(T) noexcept -> constant_wrapper<(T::value)>{ return {}; }// binary operatorstemplate<constexpr-param L, constexpr-param R>friend constexpr auto operator+(L, R) noexcept -> constant_wrapper<(L::value + R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator-(L, R) noexcept -> constant_wrapper<(L::value - R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator(L, R) noexcept -> constant_wrapper<(L::value * R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator/(L, R) noexcept -> constant_wrapper<(L::value / R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator%(L, R) noexcept -> constant_wrapper<(L::value % R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator<<(L, R) noexcept -> constant_wrapper<(L::value << R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator>>(L, R) noexcept -> constant_wrapper<(L::value >> R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator&(L, R) noexcept -> constant_wrapper<(L::value & R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator|(L, R) noexcept -> constant_wrapper<(L::value | R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator^(L, R) noexcept -> constant_wrapper<(L::value ^ R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>requires (!is_constructible_v<bool, decltype(L::value)> ||!is_constructible_v<bool, decltype(R::value)>)friend constexpr auto operator&&(L, R) noexcept -> constant_wrapper<(L::value && R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>requires (!is_constructible_v<bool, decltype(L::value)> ||!is_constructible_v<bool, decltype(R::value)>)friend constexpr auto operator||(L, R) noexcept -> constant_wrapper<(L::value || R::value)>{ return {}; }// comparisonstemplate<constexpr-param L, constexpr-param R>friend constexpr auto operator<=>(L, R) noexcept -> constant_wrapper<(L::value <=> R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator<(L, R) noexcept -> constant_wrapper<(L::value < R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator<=(L, R) noexcept -> constant_wrapper<(L::value <= R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator==(L, R) noexcept -> constant_wrapper<(L::value == R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator!=(L, R) noexcept -> constant_wrapper<(L::value != R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator>(L, R) noexcept -> constant_wrapper<(L::value > R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator>=(L, R) noexcept -> constant_wrapper<(L::value >= R::value)>{ return {}; }template<constexpr-param L, constexpr-param R>friend constexpr auto operator,(L, R) noexcept = delete; template<constexpr-param L, constexpr-param R>friend constexpr auto operator->(L, R) noexcept -> constant_wrapper<L::value->(R::value)>{ return {}; }// call and indextemplate<constexpr-param T, constexpr-param... Args>constexpr auto operator()(this T, Args...) noexceptrequires requires(Args...) { constant_wrapper<T::value(Args::value...)>(); }{ return constant_wrapper<T::value(Args::value...)>{}; }template<constexpr-param T, constexpr-param... Args>constexpr auto operator[](this T, Args...) noexcept-> constant_wrapper<(T::value[Args::value...])>{ return {}; }// pseudo-mutatorstemplate<constexpr-param T>constexpr auto operator++(this T) noexceptrequires requires(T::value_type x) { ++x; }{ return constant_wrapper<[] { auto c = T::value; return ++c; }()>{}; }template<constexpr-param T>constexpr auto operator++(this T, int) noexceptrequires requires(T::value_type x) { x++; }{ return constant_wrapper<[] { auto c = T::value; return c++; }()>{}; }template<constexpr-param T>constexpr auto operator--(this T) noexceptrequires requires(T::value_type x) { --x; }{ return constant_wrapper<[] { auto c = T::value; return --c; }()>{}; }template<constexpr-param T>constexpr auto operator--(this T, int) noexceptrequires requires(T::value_type x) { x--; }{ return constant_wrapper<[] { auto c = T::value; return c--; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator+=(this T, R) noexceptrequires requires(T::value_type x) { x += R::value; }{ return constant_wrapper<[] { auto v = T::value; return v += R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator-=(this T, R) noexceptrequires requires(T::value_type x) { x -= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v -= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator*=(this T, R) noexceptrequires requires(T::value_type x) { x *= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v *= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator/=(this T, R) noexceptrequires requires(T::value_type x) { x /= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v /= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator%=(this T, R) noexceptrequires requires(T::value_type x) { x %= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v %= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator&=(this T, R) noexceptrequires requires(T::value_type x) { x &= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v &= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator|=(this T, R) noexceptrequires requires(T::value_type x) { x |= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v |= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator^=(this T, R) noexceptrequires requires(T::value_type x) { x ^= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v ^= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator<<=(this T, R) noexceptrequires requires(T::value_type x) { x <<= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v <<= R::value; }()>{}; }template<constexpr-param T, constexpr-param R>constexpr auto operator>>=(this T, R) noexceptrequires requires(T::value_type x) { x >>= R::value; }{ return constant_wrapper<[] { auto v = T::value; return v >>= R::value; }()>{}; }};

template<cw-fixed-value X, class>struct constant_wrapper : cw-operators {static constexpr const auto & value = X.data; using type = constant_wrapper; using value_type = typename decltype(X)::type; template<constexpr-param R>constexpr auto operator=(R) const noexceptrequires requires(value_type x) { x = R::value; }{ return constant_wrapper<[] { auto v = value; return v = R::value; }()>{}; }constexpr operator decltype(auto)() const noexcept { return value; }};

1

#

The class template constant_wrapper aids in metaprogramming by ensuring that the evaluation of expressions comprised entirely of constant_wrapper are core constant expressions ([expr.const]), regardless of the context in which they appear.

In particular, this enables use of constant_wrapper values that are passed as arguments to constexpr functions to be used in constant expressions.

2

#

[Note 1:

The unnamed second template parameter to constant_wrapper is present to aid argument-dependent lookup ([basic.lookup.argdep]) in finding overloads for which constant_wrapper's wrapped value is a suitable argument, but for which the constant_wrapper itself is not.

— end note]

3

#

[Example 1: constexpr auto initial_phase(auto quantity_1, auto quantity_2) {return quantity_1 + quantity_2; }constexpr auto middle_phase(auto tbd) {return tbd; }void final_phase(auto gathered, auto available) {if constexpr (gathered == available) std::cout << "Profit!\n"; }void impeccable_underground_planning() {auto gathered_quantity = middle_phase(initial_phase(std::cw<42>, std::cw<13>)); static_assert(gathered_quantity == 55); auto all_available = std::cw<55>; final_phase(gathered_quantity, all_available); }void deeply_flawed_underground_planning() {constexpr auto gathered_quantity = middle_phase(initial_phase(42, 13)); constexpr auto all_available = 55; final_phase(gathered_quantity, all_available); // error: gathered == available// is not a constant expression} — end example]

🔗

constexpr cw-fixed-value(T (&arr)[Extent]) noexcept;

4

#

Effects: Initialize elements of data with corresponding elements of arr.