std::is_scoped_enum(3)

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

πŸ’… std::is_scoped_enum(3) πŸ’…

okay so this page is from the C++ Standard Libary (yes bestie, that’s literally how the manual name is spelled up top β€” I’m not fixing it πŸ’€), and it’s giving β€œis this type a scoped enum or not?” realness. Let’s get into it πŸ’–

πŸ’… NAME πŸ’…

std::is_scoped_enum β€” std::is_scoped_enum. That’s the name, periodt. ✨

🌸 Synopsis 🌸

Defined in header <type_traits>:

template< class T >              (since C++23)
struct is_scoped_enum;

std::is_scoped_enum is a UnaryTypeTrait.

It checks whether T is a scoped enumeration type. It provides the member constant value which is equal to true if T is a scoped enumeration type. Otherwise, value is equal to false. πŸ¦‹

⚠️ If the program adds specializations for std::is_scoped_enum or std::is_scoped_enum_v, the behavior is undefined. Like, do not do that. Undefined behavior is not the vibe. πŸ’€

πŸŽ€ Template parameters πŸŽ€

  • T β€” a type to check.

✨ Helper variable template ✨

Since C++23:

template< class T >
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;

🫢 Inherited from std::integral_constant 🫢

This trait inherits from std::integral_constant, so it’s got that integral-constant main character energy. πŸ‘‘

πŸ’Ž Member constants πŸ’Ž

  • value β€” true if T is a scoped enumeration type, 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>

🧚 Notes 🧚

Feature-test macro check, iykyk:

  • __cpp_lib_is_scoped_enum β€” value 202011L β€” std (C++23) β€” feature std::is_scoped_enum

πŸ”₯ Possible implementation πŸ”₯

template<typename E>
struct is_scoped_enum : std::bool_constant<requires
{
    requires std::is_enum_v<E>;
    requires !std::is_convertible_v<E, std::underlying_type_t<E>>;
}>
{};

πŸ’œ Example πŸ’œ

// Run this code

#include <type_traits>

class A {};

enum E {};

enum struct Es { oz };

enum class Ec : int {};

int main()
{
    static_assert(std::is_scoped_enum_v<A> == false);
    static_assert(std::is_scoped_enum_v<E> == false);
    static_assert(std::is_scoped_enum_v<Es> == true);
    static_assert(std::is_scoped_enum_v<Ec> == true);
    static_assert(std::is_scoped_enum_v<int> == false);
}

πŸ‘‘ See also πŸ‘‘

  • is_integral β€” checks if a type is an integral type (C++11) (class template)
  • is_arithmetic β€” checks if a type is an arithmetic type (C++11) (class template)
  • is_scalar β€” checks if a type is a scalar type (C++11) (class template)
  • is_enum β€” checks if a type is an enumeration type (C++11) (class template)

okay that’s the whole page, bestie. source/version/date vibe: http://cppreference.com Β· 2024.06.10 Β· std::is_scoped_enum(3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from