std::is_bounded_array(3)

πŸ“¦
version 2024.07.05 Β· 3 versions
, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::is_bounded_array(3) πŸ’–

okay so bestie, welcome to the C++ Standard Library corner of the man pages β€” this one is std::is_bounded_array and it's genuinely one of the cutest little type traits out there. no cap.

✨ NAME ✨

std::is_bounded_array β€” literally just std::is_bounded_array, that's the whole name, she said what she said πŸ’…

🌸 Synopsis 🌸

Defined in header <type_traits>:

template< class T >              (since C++20)
struct is_bounded_array;

std::is_bounded_array is a UnaryTypeTrait. (yes that's a whole category of trait, she's fancy like that πŸŽ€)

What does she actually do? She checks whether T is an array type of known bound. And she provides the member constant value, which is equal to true if T is an array type of known bound. Otherwise, value is equal to false. periodt.

⚠️ okay important warning bestie: if the program adds specializations for std::is_bounded_array or std::is_bounded_array_v, the behavior is undefined. like, don't do that. not iconic, don't try it πŸ™ˆ

🧚 Template parameters

  • T β€” a type to check

πŸ¦‹ Helper variable template

template< class T >
inline constexpr bool is_bounded_array_v =   (since C++20)
    is_bounded_array<T>::value;

⭐ Inherited from std::integral_constant

πŸ‘‘ Member constants

  • value β€” true if T is an array type of known bound, false otherwise β€” [static] (public static member constant)

πŸ’œ Member functions

  • operator bool β€” converts the object to bool, returns value (public member function)
  • operator() β€” returns value β€” (C++14) (public member function)

🌈 Member types

  • value_type β€” bool
  • type β€” std::integral_constant<bool, value>

πŸ“ Possible implementation

like, this is roughly the vibe of how she's built under the hood:

template<class T>
struct is_bounded_array : std::false_type {};

template<class T, std::size_t N>
struct is_bounded_array<T[N]> : std::true_type {};

πŸŽ€ Notes

Feature-test macro moment!! πŸ•΅οΈβ€β™€οΈ

  • __cpp_lib_bounded_array_traits β€” value 201902L β€” Std (C++20) β€” Feature: std::is_bounded_array, std::is_unbounded_array

πŸ’– Example

// Run this code

#include <iostream>
#include <type_traits>

#define OUT(...) std::cout << #__VA_ARGS__ << " : " << __VA_ARGS__ << '\n'

class A {};

int main()
{
    std::cout << std::boolalpha;
    OUT(std::is_bounded_array_v<A>);
    OUT(std::is_bounded_array_v<A[]>);
    OUT(std::is_bounded_array_v<A[3]>);
    OUT(std::is_bounded_array_v<float>);
    OUT(std::is_bounded_array_v<int>);
    OUT(std::is_bounded_array_v<int[]>);
    OUT(std::is_bounded_array_v<int[3]>);
}

Output:

std::is_bounded_array_v<A> : false
std::is_bounded_array_v<A[]> : false
std::is_bounded_array_v<A[3]> : true
std::is_bounded_array_v<float> : false
std::is_bounded_array_v<int> : false
std::is_bounded_array_v<int[]> : false
std::is_bounded_array_v<int[3]> : true

see how A[3] and int[3] ate and left zero crumbs? 🍽️ known bound = true. the [] girls with no number? false. iconic behavior honestly.

🌟 See also

  • is_array β€” checks if a type is an array type β€” (C++11) (class template)
  • is_unbounded_array β€” checks if a type is an array type of unknown bound β€” (C++20) (class template)
  • extent β€” obtains the size of an array type along a specified dimension β€” (C++11) (class template)

okay that's the whole page bestie, you're officially a bounded-array knower now 🧠✨ go forth and static_assert responsibly. mwah 🫢

source: http://cppreference.com β€” version 2024.06.10 β€” std::is_bounded_array(3) πŸ’…

πŸ“¦ where this page comes from