Last active
January 3, 2022 18:19
-
-
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
This file contains 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
// 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