Skip to content

Instantly share code, notes, and snippets.

@willeccles
Last active January 3, 2022 18:19
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 willeccles/994bf5188725e4006d81e9f2e7ec4210 to your computer and use it in GitHub Desktop.
Save willeccles/994bf5188725e4006d81e9f2e7ec4210 to your computer and use it in GitHub Desktop.
Proof of concept: generating arrays of non-moveable, non-copyable, non-default-constructible types at compile-time using increasing integer values as constructor arguments
// in action: https://godbolt.org/z/8z8r37rGP
#include <algorithm>
#include <array>
#include <iostream>
#include <tuple>
#include <utility>
class L {
public:
L()=delete;
constexpr L(int v): v_(v) {}
L(const L&)=delete;
L& operator=(const L&)=delete;
L(L&&)=delete;
L& operator=(L&&)=delete;
constexpr int get() const { return v_; };
private:
int v_;
};
template<class C, class T, T start, T incr, T... vals>
constexpr std::array<C, sizeof...(vals)> gen_array_impl(std::integer_sequence<T, vals...>) {
return std::array<C, sizeof...(vals)>{C(start + vals * incr)...};
}
template<class C, std::size_t N, int start = 0, int incr = 1>
constexpr std::array<C, N> gen_array() {
return gen_array_impl<C, int, start, incr>(std::make_integer_sequence<int, N>{});
}
int main(void) {
auto a = gen_array<L, 5, 0, 2>();
for (auto& i : a) {
std::cout << i.get() << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment