Last active
November 22, 2021 07:58
-
-
Save martinus/b9ca5335ee10f966348b38adaba9b3f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <cstddef> | |
#include <type_traits> | |
namespace basics { | |
class Times { | |
size_t m_times; | |
public: | |
constexpr explicit Times(size_t n) : m_times(n) { | |
} | |
template <typename Op> | |
constexpr void operator()(Op op) const { | |
for (size_t i = 0; i < m_times; ++i) { | |
if constexpr (std::is_invocable_v<Op, size_t>) { | |
op(i); | |
} else { | |
op(); | |
} | |
} | |
} | |
}; | |
constexpr Times operator""_times(unsigned long long int n) { | |
return Times(n); | |
} | |
void whatever(); | |
} // namespace basics | |
// usage | |
void foo() { | |
using namespace basics; | |
5_times([] { whatever(); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment