Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Concepts.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2022 Reactive Markets Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef TOOLBOX_UTIL_CONCEPTS_HPP
17#define TOOLBOX_UTIL_CONCEPTS_HPP
18
19#include <type_traits>
20
21namespace toolbox {
22inline namespace util {
23
24template <typename T>
25concept Arithmetic = std::is_arithmetic_v<T>;
26
27template <typename T>
28concept Enum = std::is_enum_v<T>;
29
30// N.B. The concept doesn't check for operator<< overloads on the streamable object.
31// Checking for operator<< overloads would've been nice, but it's not possible/difficult due
32// to recursions. For example, consider this constraint "os << int/float/etc" -- to check this
33// constraint the compiler has to check if there is a valid (unambiguous) operator<< overload.
34// However, some generic operator<< overloads for custom types will make use of this concept to
35// accept any streamable type -- this is an infinite recursion because the compiler has to then
36// check are the constraints satisfied on that overload, which means checking "os << int/float/etc"
37// constraint again, checking all operator<< overloads again, etc repeating in a cycle.
38template <typename T>
39concept Streamable = requires (T& os) {
40 os.put(std::declval<char>());
41 os.write(std::declval<const char*>(), std::declval<std::size_t>());
42};
43
44} // namespace util
45} // namespace toolbox
46
47#endif // TOOLBOX_UTIL_CONCEPTS_HPP