Namespaces
Variants

std::constant_wrapper, std::cw

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Undefined behavior utilities
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
 
Defined in header <utility>
template< auto X, class T = decltype(X) >
struct constant_wrapper;
(1) (since C++26)
template< auto X >
constexpr auto cw = std::constant_wrapper<X>{};
(2) (since C++26)
1) An empty class whose type holds the value X. A function that receives a constant_wrapper as an argument can use the wrapped value in constant expressions, such as template arguments, if constexpr conditions and static_assert declarations, although a function parameter is never a constant expression itself. This is how a compile-time value can be passed to a function through an argument, for example to a constructor, which cannot be given template arguments explicitly.
The operators of constant_wrapper apply the same operator to the wrapped values and return a constant_wrapper of the result, so std::cw<2> + std::cw<3> has type std::constant_wrapper<5>. If any operand is not a constant_wrapper, the wrapper is converted to its value instead, so std::cw<2> + x is 2 + x.
T must be decltype(X): the program is ill-formed if a specialization is instantiated with another type.
2) An object of type std::constant_wrapper<X>. This is the intended way to create a constant_wrapper.

Template parameters

X - the wrapped value: any value that can be a constant template argument, such as an integer, a floating-point value, a pointer to a function or an object of a structural class type
T - the type of X, a template parameter so that argument-dependent lookup for an expression with a constant_wrapper operand also finds the functions associated with the type of the wrapped value

Member types

Type Definition
type constant_wrapper
value_type decltype(X)

Member constants

Name Value
value
[static]
(X): a reference to the template parameter object X if value_type is a class type, a copy of X otherwise
(public static member constant)

Member functions

In the descriptions below, a constant argument is a constant_wrapper, or any other object whose type has a static data member value that can itself be wrapped in a constant_wrapper, such as a std::integral_constant.

std::constant_wrapper::operator=

template< class R >
constexpr auto operator=( R ) const noexcept
    -> std::constant_wrapper<(value = R::value)>;

Returns a constant_wrapper of the result of value = R::value.

This overload participates in overload resolution only if R is the type of a constant argument and that result is a constant expression. The wrapper itself does not change.

std::constant_wrapper::operator decltype(value)

constexpr operator decltype(value)() const noexcept;

Returns value.

std::constant_wrapper::operator()

template< class... Args >
static constexpr decltype(auto) operator()( Args&&... args )
    noexcept(/* see below */);

If every argument is a constant argument, and invoking value with the wrapped values of the arguments, as if by std::invoke, is a constant expression whose result can be wrapped, returns a constant_wrapper of that result. Otherwise, returns std::invoke(value, std::forward<Args>(args)...).

This overload participates in overload resolution only if the selected expression is valid. It is noexcept if that expression is.

std::constant_wrapper::operator[]

template< class... Args >
static constexpr decltype(auto) operator[]( Args&&... args )
    noexcept(/* see below */);

If every argument is a constant argument, and subscripting value with the wrapped values of the arguments is a constant expression whose result can be wrapped, returns a constant_wrapper of that result. Otherwise, returns value[std::forward<Args>(args)...].

This overload participates in overload resolution only if the selected expression is valid. It is noexcept if that expression is.

Non-member functions

The following operators are found only by argument-dependent lookup, when at least one operand is a constant_wrapper. Every operand must be a constant argument. Each operator applies the same operator to the wrapped values and returns a constant_wrapper of the result. It participates in overload resolution only if that result is a constant expression that can be wrapped. All of them are noexcept.

  • the unary operators +, -, ~, !, & and *;
  • the binary operators +, -, *, /, %, <<, >>, &, |, ^ and ->*;
  • the comparison operators ==, !=, <, >, <=, >= and <=>;
  • && and ||, only if the value of at least one operand cannot be converted to bool. Otherwise, the built-in operators are used, which short-circuit and return bool;
  • the increment and decrement operators, and the compound assignment operators such as +=. They are usable only for a value whose type defines these operators as const constexpr functions, such as a node of an expression template, and they do not change the operand.

The comma operator is deleted for two constant arguments.

Notes

Feature-test macro Value Std Feature
__cpp_lib_constant_wrapper 202506L (C++26) std::constant_wrapper
202603L (C++26) operator() and operator[] return plain values if they cannot return a constant_wrapper
202606L (C++26) The template parameter X is the wrapped value itself

Unlike std::integral_constant, constant_wrapper can wrap a value of any type that can be a constant template argument, and its operators keep their results in the type. The operators also accept std::integral_constant operands, as in std::cw<2> + std::integral_constant<int, 3>().

A function that should only accept wrapped values of one type can deduce the value, as in template<int N> void f(std::constant_wrapper<N>);, which accepts std::cw<5> but not std::cw<5u>.

constant_wrapper has no std::formatter specialization. To format the wrapped value, pass value instead.

Passing std::cw<f> instead of a function pointer f to std::function_ref, std::bind_front or similar function wrappers makes f part of the type that the wrapper calls. The wrapper then calls f directly instead of through a stored pointer, which saves an indirect call and lets the compiler inline f.

Example

Passes a size to a function as an argument, deduces a wrapped value, and shows which operators return a constant_wrapper.

#include <array>
#include <print>
#include <type_traits>
#include <utility>

// A function parameter is never a constant expression, but the value of a
// constant_wrapper is part of its type, so the function can still use it
auto make_filled(auto size, int value)
{
    std::array<int, size> a;
    a.fill(value);
    return a;
}

// The wrapped value can be deduced like a template argument
template<int N>
constexpr int twice(std::constant_wrapper<N>) { return 2 * N; }

constexpr int square(int x) { return x * x; }
constexpr std::array primes{2, 3, 5, 7};

int main()
{
    auto a = make_filled(std::cw<3>, 7);
    static_assert(a.size() == 3);
    std::println("make_filled(cw<3>, 7) = {}", a);
    
    static_assert(twice(std::cw<21>) == 42);
    
    // An operator whose operands are all constant_wrappers returns a
    // constant_wrapper; with any other operand, it returns a plain value
    auto five = std::cw<2> + std::cw<3>;
    static_assert(std::is_same_v<decltype(five), std::constant_wrapper<5>>);
    int x = 10;
    std::println("five = {}, five + x = {}", five.value, five + x);
    
    // Call and subscript follow the same rule
    auto sq = std::cw<square>(std::cw<4>);
    static_assert(std::is_same_v<decltype(sq), std::constant_wrapper<16>>);
    auto p = std::cw<primes>[std::cw<2>];
    static_assert(std::is_same_v<decltype(p), std::constant_wrapper<5>>);
    std::println("square(x) = {}, primes[x % 4] = {}",
                 std::cw<square>(x), std::cw<primes>[x % 4]);
}

Output:

make_filled(cw<3>, 7) = [7, 7, 7]
five = 5, five + x = 15
square(x) = 100, primes[x % 4] = 5

See also

compile-time constant of specified type with specified value
(class template) [edit]
implements compile-time sequence of integers
(class template) [edit]
non-owning wrapper of any callable object
(class template) [edit]