std::floating_point(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

Okay bestie, welcome to the std::floating_point(3) manual page β€” the C++ concept that’s basically a vibe check for floating-point types. πŸ’…

std::floating_point β€” std::floating_point. (The NAME line literally goes std::floating_point - std::floating_point, and honestly? Mirror moment. We love that for us.)


✨ Synopsis ✨

Defined in header <concepts> β€” that’s where this cutie lives.

template< class T >                                    (since C++20)
concept floating_point = std::is_floating_point_v<T>;

The concept floating_point<T> is satisfied if and only if T is a floating-point type. That’s the whole rule. No cap.

πŸŽ€ Example πŸŽ€

Run this code, bestie:

#include <concepts>
#include <iostream>
#include <type_traits>

constexpr std::floating_point auto x2(std::floating_point auto x)
{
    return x + x;
}

constexpr std::integral auto x2(std::integral auto x)
{
    return x << 1;
}

int main()
{
    constexpr auto d = x2(1.1);
    static_assert(std::is_same_v<double const, decltype(d)>);
    std::cout << d << '\n';

    constexpr auto f = x2(2.2f);
    static_assert(std::is_same_v<float const, decltype(f)>);
    std::cout << f << '\n';

    constexpr auto i = x2(444);
    static_assert(std::is_same_v<int const, decltype(i)>);
    std::cout << i << '\n';
}

πŸ¦‹ Output πŸ¦‹

2.2
4.4
888

🌈 See also 🌈

  • is_floating_point β€” checks if a type is a floating-point type (C++11) (class template).

mwah πŸ’‹ source/version: http://cppreference.com, date: 2024.06.10, manual: std::floating_point(3). byeee, go slay some templates 🫢✨

πŸ“¦ where this page comes from