concept.hpp File Reference

Utility classes for definining and using concepts lite in C++11. More...

#include <atria/estd/type_traits.hpp>
#include <atria/meta/utils.hpp>
#include <atria/meta/pack.hpp>
#include <ableton/build_system/Warnings.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/apply.hpp>
#include <algorithm>

Go to the source code of this file.

Classes

struct  models< ConceptSig, Enable >
 Returns whether a concept signature ConceptSig is satisfied. More...
 
struct  models< ConceptSpecT(Ts...), estd::void_t< decltype(std::declval< ConceptSpecT >().requires_( std::declval< Ts >()...)) > >
 
struct  models_< ConceptSpecT, T >
 
struct  if_any< T, Mfs >
 Metafunction that given a type T and a one or more of MPL metafunction classes Mfs that boolean metafunctions, returns whether any of these metafunctios Mfs returns true when applied with T. More...
 
struct  if_any< T, Mf, Mfs... >
 
struct  if_any< T, Mf >
 

Namespaces

 atria
 C++ amazing templates and reusable implementations awesomeness.
 
 atria::meta
 Metaprogramming tools, including some Booost.MPL adaptors and concept checking facilities.
 

Macros

#define ABL_CONCEPT   constexpr
 Macro for future-proof concept-lite like concepts. More...
 
#define ABL_CONCEPT_SPEC(concept_name)
 Helper macro to define concept-lite style concepts. More...
 
#define ABL_ASSERT_CONCEPT(concept, ...)
 Macro to check, at compile-time, that a concept is satisfied. More...
 
#define ABL_ASSERT_NOT_CONCEPT(concept, ...)
 Macro to check that a concept is not satisfied by some types. More...
 

Typedefs

template<bool Requirement>
using require = estd::enable_if_t< Requirement, int >
 Utility to define concepts in terms of other concepts or other kinds boolean requirements. More...
 
template<typename T , typename... Mfs>
using require_any = estd::enable_if_t< if_any< T, Mfs... >::type::value, int >
 Like require, but based on the semantics of if_any. More...
 

Functions

constexpr bool all ()
 Returns true if all the passed in values are true. More...
 
template<typename T , typename... Ts>
constexpr bool all (T x, Ts...xs)
 
template<typename... Ts>
constexpr bool valid ()
 Concept that is always satisfied by the type or family of types that is passed to it. More...
 
template<typename... Ts>
void expressions (Ts &&...)
 Allows to validate a sequence of expressions in a single decltype. More...
 
template<typename ConceptSig >
constexpr bool check ()
 Like models, but fails at compile-time when the specification is not met. More...
 
template<typename ConceptSpecT , typename... Ts>
constexpr bool check (pack< ConceptSpecT(Ts...)>)
 
template<template< typename... >class ConceptSpecT, typename... Ts>
constexpr bool check (pack< ConceptSpecT< Ts... > >)
 

Detailed Description

Utility classes for definining and using concepts lite in C++11.

C++14 introduces the notion of Concepts Lite. Concepts, as in Concepts Lite, are just a constexpr nullary function templated over T, that returns a bool indicating whether T satisfies the concept.

In this sense, require as defined in the new standard is a glorified enable_if. Also, these concepts can already be defined in C++11. The problem is, in C++11, constexpr functions are so limited that some of these functions are hard to define. This namespace provides some utilities that make concepts easier to define and use.

Here are some references where most of these techniques where borrowed from:

Definition in file concept.hpp.

Macro Definition Documentation

#define ABL_ASSERT_CONCEPT (   concept,
  ... 
)
Value:
static_assert( \
concept<__VA_ARGS__>(), \
"Concept: " #concept \
" must be satisfied by types: " #__VA_ARGS__) \

Macro to check, at compile-time, that a concept is satisfied.

It generates a static assertion that checks the given concept for the given set of types.

Definition at line 290 of file concept.hpp.

#define ABL_ASSERT_NOT_CONCEPT (   concept,
  ... 
)
Value:
static_assert( \
!concept<__VA_ARGS__>(), \
"Concept: " #concept \
" must not be satisfied by types: " #__VA_ARGS__) \

Macro to check that a concept is not satisfied by some types.

Opposite of ABL_ASSERT_CONCEPT

Definition at line 301 of file concept.hpp.

#define ABL_CONCEPT   constexpr

Macro for future-proof concept-lite like concepts.

In the future, it will expand to the concept keyword, for now it expands to constexpr. Example:

struct My_concept_spec { ... };
template <typename T>
ABL_CONCEPT bool My_concept() {
return models<My_concept_spec(T)>();
};

Definition at line 211 of file concept.hpp.

#define ABL_CONCEPT_SPEC (   concept_name)
Value:
struct concept_name ## _spec; \
template <typename... Ts> \
ABL_CONCEPT bool concept_name() \
{ \
return ::atria::meta::models<concept_name ## _spec (Ts...)>(); \
} \
struct concept_name ## _spec

Helper macro to define concept-lite style concepts.

The macro defines:

  • A concept-lite like concept named {concept_name}. In pre-C++17 compilers this is just a constexpr function template that returns bool.
  • It opens the definition of a type {concept_name}_spec that is a meant to be a concept spectification.

Example:

ABL_CONCEPT_SPEC(Equality_comparable)
{
template <typename T, typename U>
auto requires_(T&& x, U&& y) -> decltype(
bool(x != y),
bool(x == y)));
template <typename T>
auto requires_(T&& x) -> decltype(
requires_(x, x));
};
See also
models

Definition at line 243 of file concept.hpp.

Fork me on GitHub