Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
substitution_succeeds.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_CONSTRAINTS_SUBSTITUTION_SUCCEEDS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_CONSTRAINTS_SUBSTITUTION_SUCCEEDS_HPP_
7
8// IWYU pragma: private, include "arene/base/constraints.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11namespace arene {
12namespace base {
13
14// parasoft-begin-suppress AUTOSAR-A2_7_3-a "False positive: A '@brief' tag is present"
15/// @brief An alias for @c void that accepts any number of type parameters, used for substitution checks
16template <class...>
17using void_t = void;
18// parasoft-end-suppress AUTOSAR-A2_7_3-a
19
20namespace substitution_succeeds_impl {
21
22/// @brief A helper @c substitution_succeeds template used for checking if substitution
23/// into a template alias succeeds. This turns substitution successes and
24/// failures into compile-time @c true and @c false values for testing, similar
25/// to the "detection idiom" of library fundamentals TS 2
26/// (https://wg21.link/n4806). The primary usage of this facilitiy is for tests
27/// to make sure that code specifies its template constraints correctly.
28///
29/// @c substitution_succeeds_impl is an implementation-detail of
30/// @c substitution_succeeds. The default definition is picked when substitution
31/// into @c Template with @c P... fails.
32///
33///
34template <class, template <class...> class Template, class... P>
35extern constexpr bool substitution_succeeds = false;
36
37/// @brief This partial specialization is picked when substitution into @c Template
38/// with
39/// @c P... succeeds.
40///
41///
42template <template <class...> class Template, class... P>
43extern constexpr bool substitution_succeeds<void_t<Template<P...>>, Template, P...> = true;
44} // namespace substitution_succeeds_impl
45
46/// @brief Determines if a template can be instantiated successfully, similar to C++20's @c requires clause.
47/// @return true if @c Template<P...> successfully undergoes substitution when in a SFINAE context, otherwise @c false.
48///
49/// This is generally used to help emulate "concepts" in a pattern such as the following example which detects if a type
50/// has a member function named @c foo() :
51/// @snippet docs/examples/constraints_examples.cpp substitution_succeeds_usage_example
52///
53template <template <class...> class Template, class... P>
55
56} // namespace base
57} // namespace arene
58
59#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_CONSTRAINTS_SUBSTITUTION_SUCCEEDS_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr bool substitution_succeeds
Determines if a template can be instantiated successfully, similar to C++20's requires clause.
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10