Skip to content

Instantly share code, notes, and snippets.

@martinus
Last active November 22, 2021 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinus/b9ca5335ee10f966348b38adaba9b3f8 to your computer and use it in GitHub Desktop.
Save martinus/b9ca5335ee10f966348b38adaba9b3f8 to your computer and use it in GitHub Desktop.
#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