std::experi...:detected_t,(3)

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

🌸 std::experimental::is_detected, std::experimental::detected_t 🌸

okay bestie, so this is from the C++ Standard Library and it's all about the Library Fundamentals TS β€” that's a Technical Specification, iykyk 😌 it's literally the header <experimental/type_traits> and it's giving detection idiom energy ✨


✨ SYNOPSIS ✨

Defined in header <experimental/type_traits>

template< template<class...> class Op, class... Args >  (library
fundamentals TS v2)
using is_detected = /* see below */;

template< template<class...> class Op, class... Args >  (library
fundamentals TS v2)
using detected_t = /* see below */;

template< class Default, template<class...> class Op,
class... Args >                                         (library
fundamentals TS v2)
using detected_or = /* see below */;

πŸ¦‹ DESCRIPTION πŸ¦‹

The alias template detected_or is an alias for an unspecified class type with two public member typedefs value_t and type, which are defined as follows:

  • If the template-id Op<Args...> denotes a valid type, then value_t is an alias for std::true_type, and type is an alias for Op<Args...>;
  • Otherwise, value_t is an alias for std::false_type and type is an alias for Default.

The alias template is_detected is equivalent to typename detected_or<std::experimental::nonesuch, Op, Args...>::value_t. It is an alias for std::true_type if the template-id Op<Args...> denotes a valid type; otherwise it is an alias for std::false_type.

The alias template detected_t is equivalent to typename detected_or<std::experimental::nonesuch, Op, Args...>::type. It is an alias for Op<Args...> if that template-id denotes a valid type; otherwise it is an alias for the class std::experimental::nonesuch.


πŸŽ€ Additional utilities πŸŽ€

template< template<class...> class Op, class... Args >      (library
fundamentals TS
constexpr bool is_detected_v = is_detected<Op,              v2)
Args...>::value;

template< template<class...> class Op, class... Args >      (library
fundamentals TS
constexpr inline bool is_detected_v = is_detected<Op,       v3)
Args...>::value;

template< class Default, template<class...> class Op,
class... Args >                                             (library
fundamentals TS
using detected_or_t = typename detected_or<Default, Op,     v2)
Args...>::type;

template< class Expected, template<class...> class Op,
class... Args >                                             (library
fundamentals TS
using is_detected_exact = std::is_same<Expected,            v2)
detected_t<Op, Args...>>;

template< class Expected, template<class...> class Op,
class... Args >
                                                            (library
fundamentals TS
constexpr bool is_detected_exact_v =                        v2)

    is_detected_exact<Expected, Op, Args...>::value;

template< class Expected, template<class...> class Op,
class... Args >
                                                            (library
fundamentals TS
constexpr inline bool is_detected_exact_v =                 v3)

    is_detected_exact<Expected, Op, Args...>::value;

template< class To, template<class...> class Op, class...
Args >
                                                            (library
fundamentals TS
using is_detected_convertible =                             v2)

    std::is_convertible<detected_t<Op, Args...>, To>;

template< class To, template<class...> class Op, class...
Args >
                                                            (library
fundamentals TS
constexpr bool is_detected_convertible_v =                  v2)

    is_detected_convertible<To, Op, Args...>::value;

template< class To, template<class...> class Op, class...
Args >
                                                            (library
fundamentals TS
constexpr inline bool is_detected_convertible_v =           v3)

    is_detected_convertible<To, Op, Args...>::value;

The alias template is_detected_exact checks whether detected_t<Op, Args...> is Expected.

The alias template is_detected_convertible checks whether detected_t<Op, Args...> is convertible to To.

okay so quick vibe check: TS v2 and TS v3 are different versions of the Library Fundamentals TS, and the inline keyword only shows up in v3 for the _v variable templates. don't mix them up bestie, fr fr.

πŸ”₯ POSSIBLE IMPLEMENTATION πŸ”₯

namespace detail
{
    template<class Default, class AlwaysVoid, template<class...> class
Op, class... Args>
    struct detector
    {
        using value_t = std::false_type;
        using type = Default;
    };

    template<class Default, template<class...> class Op, class... Args>
    struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
    {
        using value_t = std::true_type;
        using type = Op<Args...>;
    };
} // namespace detail

template<template<class...> class Op, class... Args>
using is_detected = typename detail::detector<nonesuch, void, Op,
Args...>::value_t;

template<template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op,
Args...>::type;

template<class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;

πŸ’– EXAMPLE πŸ’–

// Run this code

#include <cstddef>
#include <experimental/type_traits>

template<class T>
using copy_assign_t = decltype(std::declval<T&>() = std::declval<const
T&>());

struct Meow {};
struct Purr { void operator=(const Purr&) = delete; };

static_assert(std::experimental::is_detected<copy_assign_t,
Meow>::value,
              "Meow should be copy assignable!");
static_assert(!std::experimental::is_detected_v<copy_assign_t, Purr>,
              "Purr should not be copy assignable!");
static_assert(std::experimental::is_detected_exact_v<Meow&,
copy_assign_t, Meow>,
              "Copy assignment of Meow should return Meow&!");

template<class T>
using diff_t = typename T::difference_type;

template<class Ptr>
using difference_type =
std::experimental::detected_or_t<std::ptrdiff_t, diff_t, Ptr>;

struct Woof { using difference_type = int; };
struct Bark {};

static_assert(std::is_same<difference_type<Woof>, int>::value,
              "Woof's difference_type should be int!");
static_assert(std::is_same<difference_type<Bark>,
std::ptrdiff_t>::value,
              "Bark's difference_type should be ptrdiff_t!");

int main() {}
ngl this example is iconic β€” Meow is copy assignable (slay), Purr deleted its copy assignment so it's not (rip), and Woof has a difference_type of int while Bark falls back to std::ptrdiff_t. the template literally detects all of it. periodt. πŸ’…

🧚 SEE ALSO 🧚

(the original page doesn't list any SEE ALSO refs, bestie β€” that's just how this one is)


source: cppreference.com 🌈 2024.06.10 β€” mwah, hope this helped, go slay those template metaprograms 🫢✨

πŸ“¦ where this page comes from