concepts.hpp

Useful concepts that are not trivially available in the C++20 standard library.

This file is part of Empirical, https://github.com/devosoft/Empirical Copyright (C) 2023-2024 Michigan State University MIT Software license; see doc/LICENSE.md

The concepts available here are:

canStreamTo<STREAM_T, OBJECT_T> canStreamFrom<STREAM_T, OBJECT_T>

Defines

INCLUDE_EMP_BASE_CONCEPTS_HPP_GUARD

Typedefs

template<typename T>
using is_std_function = is_template<T, std::function>
template<typename T>
using is_std_vector = is_template<T, std::vector>
template<typename T>
using is_emp_vector = is_template<T, vector>
template<typename T>
using is_span = is_template_tn<T, std::span>

Variables

template<typename T> concept has_size   = requires(T & v) { v.size(); }
template<typename T> concept has_resize   = requires(T & v, size_t size) { v.resize(size); }
template<typename T> concept has_begin   = requires(T & v) { v.begin(); }
template<typename T> concept has_end   = requires(T & v) { v.end(); }
template<typename STREAM_T, typename OBJ_T> concept canStreamTo  = requires(STREAM_T & stream, OBJ_T value) {{ stream << value } -> std::convertible_to<std::ostream &>;}

Concept to identify if a type can be sent into an ostream.

template<typename STREAM_T, typename OBJ_T> concept canStreamFrom  = requires(STREAM_T & stream, OBJ_T value) {{ stream >> value } -> std::convertible_to<std::istream &>;}

Concept to identify if a type can be set from an istream.

template<typename OBJ_T> concept hasToString  = requires(OBJ_T value) {{value.ToString() } -> std::same_as<std::string>;}

Does a type have a ToString() member function?

template<typename OBJ_T> concept hasToDouble  = requires(OBJ_T value) {{value.ToDouble() } -> std::same_as<double>;}

Does a type have a ToDouble() member function?

template<typename T> concept hasFromString  = requires(T t, const std::string & s, const char * c) {{ t.FromString(s) };{ t.FromString(c) };}

Does a type have a FromString() member function?

template<typename T> concept hasFromDouble  = requires(T t, double d) {{t.FromDouble(d) };}

Does a type have a FromString() member function?

template<typename T, template<typename...> class TEMPLATE>
struct is_template : public std::false_type
#include <concepts.hpp>

Test if a given type T is an instance of template TEMPLATE.

template<template<typename...> class TEMPLATE, typename ...ARG_Ts>
struct is_template<TEMPLATE<ARG_Ts...>, TEMPLATE> : public std::true_type
#include <concepts.hpp>
template<typename T, template<typename, auto> class TEMPLATE>
struct is_template_tn : public std::false_type
#include <concepts.hpp>

Test if a given type T is an instance of template TEMPLATE with one type and one non-type arg.

template<template<typename, auto> class TEMPLATE, typename ARG1, auto ARG2>
struct is_template_tn<TEMPLATE<ARG1, ARG2>, TEMPLATE> : public std::true_type
#include <concepts.hpp>
template<typename T>
class is_streamable : public std::bool_constant<canStreamTo<std::ostream, T>>
#include <_is_streamable.hpp>

A type trait to identify if a type is streamable (for backward compatibility)

template<typename T>
struct IsIterable : public std::bool_constant<std::ranges::range<T>>
#include <concepts.hpp>

A type trait to identify if a type is iterable.