Loading...
Searching...
No Matches
constforeach.h
Go to the documentation of this file.
1#ifndef _BBM_CONSTFOREACH_H_
2#define _BBM_CONSTFOREACH_H_
3
4/************************************************************************/
5/*! \file constforeach.h
6 \brief Compile-time for each loop
7
8 Expects the body to passed as a templated lambda that takes the typenames
9 and no arguments.
10
11 Example:
12 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
13 constforeach<Ts...>( [&]<typename T>()
14 {
15 std::cerr << typestring<T> << std::endl;
16 });
17 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 Prints the typestrings of all typenames in Ts.
19
20 A helper macro simplifies the call:
21 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
22 CONSTFOREACH(T, TS,
23 {
24 std::cerr << typestring<T> << std::endl;
25 });
26 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27
28*************************************************************************/
29
30namespace bbm {
31
32 /**********************************************************************/
33 /*! \brief Concept to check if a lambda functions meets the required signature
34 **********************************************************************/
35 template<typename F, typename... T>
36 concept has_constforeach_lambda = requires(F&& f)
37 {
38 (f.template operator()<T>(), ...);
39 };
40
41 /**********************************************************************/
42 /*! \brief constforeach over all typenames in T.
43 **********************************************************************/
44 template<typename... T, typename F> requires has_constforeach_lambda<F, T...>
45 inline constexpr void constforeach(F&& f)
46 {
47 (f.template operator()<T>(), ...);
48 }
49
50 /**********************************************************************/
51 /*! \brief HELPER MACRO
52 **********************************************************************/
53 #define CONSTFOREACH(ITR_TYPE, TYPE_LIST, ...) if constexpr (sizeof...(TYPE_LIST) > 0) bbm::constforeach<TYPE_LIST...>( [&]<typename ITR_TYPE>() { __VA_ARGS__ } );
54
55} // end bbm namespace
56
57
58#endif /* _BBM_CONSTFOREACH_H_ */
Concept to check if a lambda functions meets the required signature.
Definition: constforeach.h:36
Definition: aggregatebsdf.h:29
constexpr void constforeach(F &&f)
constforeach over all typenames in T.
Definition: constforeach.h:45