generate consecutive numbers at compile time
template
struct Elem{
enum { res = 1 + Elem::res };
};
template<>
struct Elem<0> {
enum { res = 0 };
};
template
struct A {
static constexpr auto& array =
A::res, args...>::array;
};
template
struct A<0, args...> {
static constexpr int array[] = { 0, args... };
};
template
static constexpr auto& Array = A::array;
//пример использования
int main()
{
constexpr unsigned N = 8;
for (unsigned n : Array)
cout << n << ' ';
//0 1 2 3 4 5 6 7 8
}
generate consecutive numbers at compile time
template
struct Elem{
enum { res = 1 + Elem::res };
};
template<>
struct Elem<0> {
enum { res = 0 };
};
template
struct A {
static constexpr auto& array =
A::res, args...>::array;
};
template
struct A<0, args...> {
static constexpr int array[] = { 0, args... };
};
template
static constexpr auto& Array = A::array;
//пример использования
int main()
{
constexpr unsigned N = 8;
for (unsigned n : Array)
cout << n << ' ';
//0 1 2 3 4 5 6 7 8
}
|