컴파일 타임 배열을 얻으려고하므로 this answer으로 오십시오.메타 프로그래밍에 관한 몇 가지 질문이 있으십니까?
struct gens : gens<N-1, N-1, S...>
의 구문은 무엇인가 : 그래서 여기에 두 가지 질문이 있습니다,#include <array> #include <algorithm> #include <iterator> #include <iostream> template<int ...> struct seq { }; template<int N, int ...S> struct gens : gens<N-1, N-1, S...> { }; template<int ...S> struct gens<0, S...> { typedef seq<S...> type; }; constexpr int f(int n) { return n; } template <int N> class array_thinger { typedef typename gens<N>::type list; template <int ...S> static constexpr std::array<int,N> make_arr(seq<S...>) { return std::array<int,N>{{f(S)...}}; } public: static constexpr std::array<int,N> arr = make_arr(list()); }; template <int N> constexpr std::array<int,N> array_thinger<N>::arr; int main() { std::copy(begin(array_thinger<10>::arr), end(array_thinger<10>::arr), std::ostream_iterator<int>(std::cout, "\n")); }
하지만 메타 프로그래밍에 대한 안돼서 : 아래는 대답에서 코드인가? C++ 0x에서
Delegating constructors
처럼 보이지만 잘 모르겠습니다.struct seq
및typedef seq<S...> type
의 용도는 무엇입니까? 아, 나는 또한 템플릿에 대한 좋은 명령이 없습니다.
** 1 ** 그것은 상속입니다. 'gens'은'gens '** 2에서 파생됩니다. ** 평범한'typedef'입니다. 'type' 이름은'seq '의 별칭으로 선언되었습니다. –
예제는 divide와 conquer를 사용한 재귀 인 정수 시퀀스의보다 복잡한 구현입니다. 대규모의 인스턴스화 깊이를 피하는 데 사용됩니다. –
더 간단한 예를 보려면 [this] (https://stackoverflow.com/a/27125297/4832499)를 참조하십시오. –