Corrade/Utility/TypeTraits.h file

Macros CORRADE_HAS_TYPE(), alias Corrade::Utility::IsIterable.

Contents

Namespaces

namespace Corrade
Root namespace.
namespace Corrade::Utility
Utitlities.

Typedefs

template<class T>
using IsIterable = std::integral_constant<bool, implementation-specific>
Traits class for checking whether given type is iterable.
template<class T>
using IsStringLike = std::integral_constant<bool, implementation-specific> new in 2019.10
Traits class for checking whether given type is iterable.

Defines

#define CORRADE_STD_IS_TRIVIALLY_TRAITS_SUPPORTED new in Git master
Whether the std::is_trivially_copyable family of type traits is supported by the standard library.
#define CORRADE_HAS_TYPE(className, typeExpression)
Macro for creating traits class that checks for type expression validity.

Define documentation

#define CORRADE_STD_IS_TRIVIALLY_TRAITS_SUPPORTED new in Git master

Whether the std::is_trivially_copyable family of type traits is supported by the standard library.

The std::is_trivially_constructible, std::is_trivially_default_constructible, std::is_trivially_copy_constructible, std::is_trivially_move_constructible, std::is_trivially_assignable, std::is_trivially_copy_assignable, std::is_trivially_move_assignable family of traits is not implemented on libstdc++ below version 5. Instead, legacy std::has_trivial_default_constructor, std::has_trivial_copy_constructor and std::has_trivial_copy_assign are available, with slightly different semantics (see e.g. https://stackoverflow.com/q/12754886 for more information). From libstdc++ 5 onwards these are marked as deprecated and libstdc++ 7 removes them, so alternatively there are __has_trivial_constructor(), __has_trivial_copy() and __has_trivial_assign() builtins that don't produce any deprecated warnings and are available until GCC 9 / Clang 10 at least — however note that for SFINAE you need to wrap them in std::integral_constant as otherwise GCC would throw errors similar to the following:

error: use of built-in trait ‘__has_trivial_copy(T)’ in function signature; use library traits instead

This macro is defined if the standard variants are available. Unfortunately, when libstdc++ is used through Clang, there's no way to check for its version until libstdc++ 7, which added the _GLIBCXX_RELEASE macro. That means, when using Clang with libstdc++ 5 or 6, it will still report those traits as being unavailable. Both libc++ and MSVC STL have these traits in all versions supported by Corrade, so there the macro is defined always.

#define CORRADE_HAS_TYPE(className, typeExpression)

Macro for creating traits class that checks for type expression validity.

Parameters
className Resulting class name
typeExpression Type expression to check

Defines a traits class checking whether typeExpression is valid. You can use T to reference the type which is being checked. The defined class is then implicitly convertible to bool holding the result.

Usage examples: checking for presence of key_type member typedef:

CORRADE_HAS_TYPE(HasKeyType, typename T::key_type);

static_assert(HasKeyType<std::map<int, int>>::value, "");
static_assert(!HasKeyType<std::vector<int>>::value, "");

Checking for presence of size() member function:

CORRADE_HAS_TYPE(HasSize, decltype(std::declval<T>().size()));

static_assert(HasSize<std::vector<int>>::value, "");
static_assert(!HasSize<std::tuple<int, int>>::value, "");