Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
AMReX_ConstexprFor.H
Go to the documentation of this file.
1#ifndef AMREX_CONSTEXPR_FOR_H_
2#define AMREX_CONSTEXPR_FOR_H_
3#include <AMReX_Config.H>
4
6#include <AMReX_Extension.H>
7
8#include <type_traits>
9
10namespace amrex {
11
12// Implementation of "constexpr for" based on
13// https://artificial-mind.net/blog/2020/10/31/constexpr-for
14//
15// Approximates what one would get from a compile-time
16// unrolling of the loop
17// for (int i = 0; i < N; ++i) {
18// f(i);
19// }
20//
21// The mechanism is recursive: we evaluate f(i) at the current
22// i and then call the for loop at i+1. f() is a lambda function
23// that provides the body of the loop and takes only an integer
24// i as its argument.
25
26template<auto I, auto N, class F>
28constexpr void constexpr_for (F const& f)
29{
30 if constexpr (I < N) {
31 f(std::integral_constant<decltype(I), I>());
32 constexpr_for<I+1, N>(f);
33 }
34}
35
36}
37
38#endif
#define AMREX_INLINE
Definition AMReX_Extension.H:127
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Definition AMReX_Amr.cpp:49
AMREX_GPU_HOST_DEVICE AMREX_INLINE constexpr void constexpr_for(F const &f)
Definition AMReX_ConstexprFor.H:28