Skip to content

Group scl_utility_type_traits

Modules > scl_utility_type_traits

Type-level utilities for C++ metaprogramming.

Files

Type Name
file detection.h
Implementation of C++ Standard Proposal N4502 - Detection Idiom.
file forward_like.h
Helpers for forwarding objects with combined cv/ref qualifiers ( forward_like_t ).
file member_like.h
Traits to build pointers to class members (C++20).
file method.h
Provides macros for generating member function detection traits.
file operator.h
Operator detection traits: compile-time aliases and value checks (Detection Idiom)
file overload_cast.h
Utility for disambiguating function and member function overloads.

Public Types

Type Name
typedef detail::detector< Default, void, Operation, Arguments... > detected_or
Helper for getting detected type or a default.
typedef typename detected_or< Default, Operation, Arguments... >::type detected_or_t
Gets the type of Operation<Arguments...> or Default if ill-formed.
typedef typename detail::detector< void, void, Operation, Arguments... >::type detected_t
Gets the type of Operation<Arguments...> or void if ill-formed.
typedef ::scl::detail::add_reference_like_t< Base, ::scl::detail::add_cv_from_t< Base, ::std::remove_reference_t< Type > > > forward_like_t
Make a type Type "look like"Base for cv-ref qualifiers (matches ::std::forward_like rules).
typedef typename detail::detector< void, void, Operation, Arguments... >::value_t is_detected
Detects whether Operation<Arguments...> is a valid expression.
typedef ::std::is_convertible< detected_t< Operation, Arguments... >, To > is_detected_convertible
Checks if Operation<Arguments... > is convertible to To.
typedef ::std::is_same< Expected, detected_t< Operation, Arguments... > > is_detected_exact
Checks if Operation<Arguments...> results in exactly Expected type.
typedef typename ::scl::detail::member_function_like< Type, Signature >::type member_function_like_t
Pointer-to-member-function type for class Type and function Signature, where the member function's cv/ref qualifiers are copied from Type.
typedef typename ::scl::detail::member_like< Type, Member >::type member_like_t
Generic pointer-to-member type selector for class Type and Member.
typedef typename ::scl::detail::member_property_like< Type, Member >::type member_property_like_t
Pointer-to-data-member type for class Type and member type Member.

Public Attributes

Type Name
constexpr bool is_detected_convertible_v = is\_detected\_convertible&lt;To, Operation, Arguments...&gt;::value
Variable template version of is_detected_convertible.
constexpr bool is_detected_exact_v = is\_detected\_exact&lt;Expected, Operation, Arguments...&gt;::value
Variable template version of is_detected_exact.
constexpr bool is_detected_v = is\_detected&lt;Operation, Arguments...&gt;::value
Variable template version of is_detected.
constexpr detail::overload_cast< Args... > overload_cast = {}
A helper utility to select a specific overload of a function or member function.

Public Functions

Type Name
constexpr decltype(auto) forward_like (T && t) noexcept
Forwards a value with the cv-ref qualifiers of Base applied toT 's type, matching ::std::forward_like semantics.

Macros

Type Name
define SCL_FUNCTION_DETECTION (name) /* multi line expression */
Generates a suite of traits to detect the existence of a specific free function.
define SCL_METHOD_DETECTION (name)
Generates a suite of traits to detect the existence of a specific member method.

Public Types Documentation

typedef detected_or

Helper for getting detected type or a default.

using scl::detected_or = typedef detail::detector<Default, void, Operation, Arguments...>;

Template parameters:

  • Default Type to use if detection fails
  • Operation Template to instantiate
  • Arguments Arguments for the template

Provides nested type 'type' which is either Operation<Arguments...> if well-formed, or Default otherwise.


typedef detected_or_t

Gets the type of Operation<Arguments...> or Default if ill-formed.

using scl::detected_or_t = typedef typename detected_or<Default, Operation, Arguments...>::type;

Template parameters:

  • Default Type to use if detection fails
  • Operation Template to instantiate
  • Arguments Arguments for the template
using result = detected_or_t<int, has_foo_t, MyClass>;

typedef detected_t

Gets the type of Operation<Arguments...> or void if ill-formed.

using scl::detected_t = typedef typename detail::detector<void, void, Operation, Arguments...>::type;

Template parameters:

  • Operation Template to instantiate
  • Arguments Arguments for the template
using result = detected_t<has_foo_t, MyClass>;

typedef forward_like_t

Make a type Type "look like"Base for cv-ref qualifiers (matches ::std::forward_like rules).

using scl::forward_like_t = typedef ::scl::detail::add_reference_like_t<Base, ::scl::detail::add_cv_from_t<Base, ::std::remove_reference_t<Type> >>;

Template parameters:

  • Base Type providing qualifiers/ref to propagate.
  • Type Type to which qualifiers/ref are applied.

Semantics: * CV-qualifiers: Applies const and volatile from Base onto remove_reference_t<Type>. (i.e. union of qualifiers, not replacement) * Reference: Applies reference category of Base (if any). If Base is lvalue ref, result is lvalue ref; if rvalue ref, rvalue ref; else no reference. Reference is omitted for void.

** **

using P0 = forward_like_t<int const &, double>            // double const &
using P1 = forward_like_t<int &&, float const>            // float const &&
using P2 = forward_like_t<int volatile, char>             // char volatile
using P3 = forward_like_t<int const volatile &, short>    // short const volatile &
using P4 = forward_like_t<int, double&&>                  // double

typedef is_detected

Detects whether Operation<Arguments...> is a valid expression.

using scl::is_detected = typedef typename detail::detector<void, void, Operation, Arguments...>::value_t;

Template parameters:

  • Operation Template to instantiate
  • Arguments Arguments for the template

Alias to std::true_type if Operation<Arguments...> is well-formed, std::false_type otherwise.

template <typename T>
using has_foo_t = decltype(std::declval<T>().foo());

static_assert(is_detected<has_foo_t, MyClass>::value);

typedef is_detected_convertible

Checks if Operation<Arguments... > is convertible to To.

using scl::is_detected_convertible = typedef ::std::is_convertible<detected_t<Operation, Arguments...>, To>;

Template parameters:

  • To Target type for conversion
  • Operation Template to instantiate
  • Arguments Arguments for the template
static_assert(is_detected_convertible<double, get_value_t, MyClass>::value);

typedef is_detected_exact

Checks if Operation<Arguments...> results in exactly Expected type.

using scl::is_detected_exact = typedef ::std::is_same<Expected, detected_t<Operation, Arguments...> >;

Template parameters:

  • Expected The type to compare against
  • Operation Template to instantiate
  • Arguments Arguments for the template
static_assert(is_detected_exact<int, get_value_type_t, MyClass>::value);

typedef member_function_like_t

Pointer-to-member-function type for class Type and function Signature, where the member function's cv/ref qualifiers are copied from Type.

using scl::member_function_like_t = typedef typename ::scl::detail::member_function_like<Type, Signature>::type;

Template parameters:

  • Type Object type whose cv/ref qualifiers will be mirrored onto the member function. The class type itself is decayed with ::::std::remove_cvref_t before forming the pointer-to-member.
  • Signature Non-static member function signature as a function type, e.g. Result(Arguments...) or Result(Arguments...) noexcept.

** **

struct Foo {};
using P0 = scl::member_function_like_t<Foo, void(int)>;           // void (Foo::*)(int)
using P1 = scl::member_function_like_t<Foo&, void(int)>;          // void (Foo::*)(int) &
using P2 = scl::member_function_like_t<Foo const, void(int)>;     // void (Foo::*)(int) const
using P3 = scl::member_function_like_t<Foo const&&, void() noexcept>; // void (Foo::*)() const && noexcept

typedef member_like_t

Generic pointer-to-member type selector for class Type and Member.

using scl::member_like_t = typedef typename ::scl::detail::member_like<Type, Member>::type;

Selects the appropriate pointer-to-member type based on whether Member is a function type. * If Member is a function type like Result(Arguments...) or Result(Arguments...) noexcept, this aliases to scl::member_function_like_t<Type, Member>, producing: Result (::std::remove_cvref_t<Type>::*)(Arguments...) [cv] [ref] [noexcept] where: * [cv]/[ref] are mirrored from Type (const/volatile and &/&&), * [noexcept] is mirrored from T.

  • Otherwise, it aliases to scl::member_property_like_t<Type, Member>, producing: Member (::std::remove_cvref_t<Type>::*) (see member_property_like_t for details on how object qualifiers may affect Member).

Template parameters:

  • Type Class (object) type whose cv/ref qualifiers are mirrored for member functions. The class itself is decayed with ::std::remove_cvref_t before forming the pointer type.
  • Member Either a function type (for member functions) or a non-function type (for data members).

** **

struct Foo {};

// Function members
using M0 = scl::member_like_t<Foo, void(int)>;              // void (Foo::*)(int)
using M1 = scl::member_like_t<Foo const&, void() noexcept>; // void (Foo::*)() const & noexcept

// Data members
using M2 = scl::member_like_t<Foo, int>;        // int Foo::*
using M3 = scl::member_like_t<Foo const, int>;  // int const Foo::*

typedef member_property_like_t

Pointer-to-data-member type for class Type and member type Member.

using scl::member_property_like_t = typedef typename ::scl::detail::member_property_like<Type, Member>::type;

Forms the type Member (::std::remove_cvref_t<Type>::*). Object cv/ref qualifiers do not change the pointer type for data members; they only affect access expressions.

** **

using P0 = scl::member_property_like_t<Foo, int>;         // int Foo::*
using P1 = scl::member_property_like_t<Foo const&, int>;  // int const & Foo::*
using P2 = scl::member_property_like_t<Foo, int const>;   // int Foo::*

Public Attributes Documentation

variable is_detected_convertible_v

Variable template version of is_detected_convertible.

constexpr bool scl::is_detected_convertible_v;

Template parameters:

  • To Target type for conversion
  • Operation Template to instantiate
  • Arguments Arguments for the template

variable is_detected_exact_v

Variable template version of is_detected_exact.

constexpr bool scl::is_detected_exact_v;

Template parameters:

  • Expected The type to compare against
  • Operation Template to instantiate
  • Arguments Arguments for the template

variable is_detected_v

Variable template version of is_detected.

constexpr bool scl::is_detected_v;

Template parameters:

  • Operation Template to instantiate
  • Arguments Arguments for the template
if constexpr (is_detected_v<has_foo_t, MyClass>) { ... }

variable overload_cast

A helper utility to select a specific overload of a function or member function.

constexpr detail::overload_cast<Args...> scl::overload_cast;

This template class provides a set of call operators that disambiguate overloaded functions based on their argument types (Args...) and qualifiers (const, volatile, lvalue/rvalue references, and noexcept).

Template parameters:

  • Args The exact argument types used to identify the desired overload.
struct X {
  void foo(int) &;
  void foo(double) const &;
  void foo(int) const &&;
};

void bar(int);
void bar(double) noexcept;

auto pf1 = overload_cast<int>(&X::foo);        // selects void foo(int) &
auto pf2 = overload_cast<double>(&X::foo);     // selects void foo(double) const &
auto f1  = overload_cast<int>(&bar);           // selects void bar(int)
auto f2  = overload_cast<double>(&bar);        // selects void bar(double) noexcept

Public Functions Documentation

function forward_like

Forwards a value with the cv-ref qualifiers of Base applied toT 's type, matching ::std::forward_like semantics.

template<typename Base, typename T>
constexpr decltype(auto) forward_like (
    T && t
) noexcept

Example: forward_like<const T&, x>(some_val) will forward some_val as a const lvalue reference if T is const.


Macro Definition Documentation

define SCL_FUNCTION_DETECTION

Generates a suite of traits to detect the existence of a specific free function.

#define SCL_FUNCTION_DETECTION (
    name
) `/* multi line expression */`

For a given name foo, this macro generates:

  • Basic Detection (Supports ADL):
  • foo_function_operation<Args...>: The detection alias using foo(declval<Args>()...).
  • has_foo_function_v<Args...>: Boolean constant.
  • foo_function_t<Args...>: Resulting return type.

  • Exact Detection (Checks for a specific symbol pointer):

  • foo_function_exact_operation<Args...>: Uses &foo to verify a specific function exists.
  • has_foo_function_exact_v<Args...>: Boolean constant.
  • foo_function_exact_t<Args...>: Resulting return type.

Parameters:

  • name The name of the function to detect.

Warning:

CRITICAL: This macro MUST be invoked AFTER the target function has been declared. Since the macro expansion takes the address of the function (&name), the symbol must be visible to the compiler at the point of expansion, or a compilation error will occur.


define SCL_METHOD_DETECTION

Generates a suite of traits to detect the existence of a specific member method.

#define SCL_METHOD_DETECTION (
    name
) 

For a given name foo, this macro generates:

  • Basic Detection (Checks if val.foo(args...) is valid):
  • foo_method_operation<Type, Args...>: The detection alias.
  • has_foo_method_v<Type, Args...>: Boolean constant.
  • foo_method_t<Type, Args...>: Resulting return type (or nonesuch).

  • Exact Member Detection (Checks &Type::foo existence and ref-qualifiers):

  • foo_method_exact_operation<Type, Args...>: The exact detection alias.
  • has_foo_method_exact_v<Type, Args...>: Boolean constant.
  • foo_method_exact_t<Type, Args...>: Resulting return type.

  • Basic Static Detection (Checks if Type::foo(args...) is valid):

  • foo_static_method_operation<Type, Args...>: The detection alias.
  • has_foo_static_method_v<Type, Args...>: Boolean constant.
  • foo_static_method_t<Type, Args...>: Resulting return type (or nonesuch).

  • Exact Static Member Detection (Checks &Type::foo existence):

  • foo_static_method_exact_operation<Type, Args...>: The exact detection alias.
  • has_foo_static_method_exact_v<Type, Args...>: Boolean constant.
  • foo_static_method_exact_t<Type, Args...>: Resulting return type.

Parameters:

  • name The name of the member function to detect.