Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Last active December 12, 2019 13:54
Show Gist options
  • Save disconnect3d/13c2d035bb31b244df14 to your computer and use it in GitHub Desktop.
Save disconnect3d/13c2d035bb31b244df14 to your computer and use it in GitHub Desktop.
C++ template for loop which use loop counter as template argument
#include <iostream>
#include <array>
template<size_t c>
struct ForLoop {
template<template <size_t> class Func>
static void iterate() {
Func<c>()();
ForLoop<c-1>::template iterate<Func>();
}
};
template<>
struct ForLoop<0> {
template<template <size_t> class Func>
static void iterate() {
Func<0>()();
}
};
template <size_t size>
struct Foo {
void operator()() {
std::array<int, size> arr;
std::cout << "Array size: " << arr.size() << std::endl;
}
};
int main() {
ForLoop<4>::iterate<Foo>();
}
@disconnect3d
Copy link
Author

Useful materials:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment