Namespaces
Variants

std::wide_result

From cppreference.com
 
 
 
Defined in header <numeric>
template< class T >
class wide_result;
(since C++29)

std::wide_result<T> is a class template that provides a way to store as a single unit the double-width result of widening arithmetic operations, such as std::widening_clmul, without requiring a built-in integer type twice as wide as T. Every specialization of wide_result is a standard-layout, trivially copyable class type.

Template parameters

T - the underlying integer type
Type requirements
-
T must be an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type); otherwise, the specialization is ill-formed.

Data members

Member Description
T high-bits (private) keeps higher bits of the result
(exposition-only member object*)
T low-bits (private) keeps lower bits of the result
(exposition-only member object*)

The layout of wide_result is not specified: implementations can store these members in either order (for example, to match native endianness) and can over-align specializations (such as aligning std::wide_result<std::uint64_t> to 16 bytes). The comparison operators compare the high bits first regardless.

Member functions

constructs a wide_result
(public member function)
returns the higher bits of result
(public member function)
returns the lower bits of result
(public member function)

std::wide_result::wide_result

constexpr wide_result( T high, T low ) noexcept;
(since C++29)

Initializes high-bits with high and low-bits with low.

Parameters

high - higher bits to be stored in the wide result
low - lower bits to be stored in the wide result

std::wide_result::high

constexpr T high() const noexcept;
(since C++29)

An accessor member function. Returns high-bits.

std::wide_result::low

constexpr T low() const noexcept;
(since C++29)

An accessor member function. Returns low-bits.

Non-member functions

compares two wide_result objects
(function)
three-way compares two wide_result objects
(function)

operator==(std::wide_result)

friend constexpr bool operator==( const wide_result& x,
                                  const wide_result& y ) = default;
(since C++29)

Equivalent to return x.high() == y.high() and x.low() == y.low();.

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::wide_result is an associated class of the arguments.

The != operator is synthesized from operator==.

Parameters

x, y - wide result objects to compare

operator<=>(std::wide_result)

friend constexpr std::strong_ordering operator<=>( const wide_result& x,
                                                   const wide_result& y ) = default;
(since C++29)

The three-way comparison of given wide result objects. Compares high() first, then low(), behaving as if comparing two unsigned integers twice as wide as T.

This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::wide_result is an associated class of the arguments.

Parameters

x, y - wide result objects to compare lexicographically

Standard library

The following standard library functions use std::wide_result as the return type:

Numeric functions
computes the widening carry-less product
(function template) [edit]

Synopsis

namespace std {
    template<class T>
    class wide_result
    {
    public:
        constexpr wide_result(T high, T low) noexcept;
        constexpr T high() const noexcept;
        constexpr T low() const noexcept;
        friend constexpr bool operator==(const wide_result&,
                                         const wide_result&) = default;
        friend constexpr strong_ordering operator<=>(const wide_result& x,
                                                     const wide_result& y) = default;
    private:
        T /*high-bits*/, /*low-bits*/; // exposition-only
    };
}

Notes

Because std::wide_result provides defaulted operator== and a lexicographical operator<=>, it can be directly asserted in tests or stored in ordered containers as if it were an unsigned integer twice as wide as T.

Feature-test macro Value Std Feature
__cpp_lib_clmul 202611L (C++29) std::clmul, std::widening_clmul, and std::wide_result

Example

#include <cstdint>
#include <numeric>

int main()
{
    // 0x8000'0000'0000'0000 * 0x0000'0000'0000'0002 in GF(2)[X]
    // shifts the MSB into bit 64 (the lowest bit of high())
    constexpr auto res = std::widening_clmul<std::uint64_t>(1ULL << 63, 2ULL);

    static_assert(res.high() == 1ULL);
    static_assert(res.low() == 0ULL);
    static_assert(res == std::wide_result<std::uint64_t>{1ULL, 0ULL});
    static_assert(res > std::wide_result<std::uint64_t>{0ULL, ~0ULL});
}

See also

provides a way to store two objects or references of the same type as a single unit
(class template) [edit]
computes quotient and remainder of integer division
(function) [edit]
implements binary tuple, i.e. a pair of values
(class template) [edit]