在我看來,你需要整數的兩個列表,出來的1
階段如果你定義一個簡單的模板整數容器(在C++ 14可以使用std::integer_sequence
)
template <int...>
struct iList
{ };
可以定義基類如下(對不起:使用Eigen::Matrix
foo
代替)
template <typename, typename, typename = std::tuple<>>
struct NetBase;
// avoid the first couple
template <int ... Is, int J0, int ... Js>
struct NetBase<iList<0, Is...>, iList<J0, Js...>, std::tuple<>>
: NetBase<iList<Is...>, iList<Js...>, std::tuple<>>
{ };
// intermediate case
template <int I0, int ... Is, int J0, int ... Js, typename ... Ts>
struct NetBase<iList<I0, Is...>, iList<J0, Js...>, std::tuple<Ts...>>
: NetBase<iList<Is...>, iList<Js...>,
std::tuple<Ts..., foo<float, I0, J0>>>
{ };
// avoid the last couple and terminate
template <int I0, typename ... Ts>
struct NetBase<iList<I0>, iList<0>, std::tuple<Ts...>>
{ using type = std::tuple<Ts...>; };
和0簡直成了(觀察出相夫婦整數列表中的)
template <int F, int S, int... Os>
struct Net : NetBase<iList<0, F, S, Os...>, iList<F, S, Os..., 0>>
{ };
下面是一個完整的編譯例子
#include <tuple>
template <int...>
struct iList
{ };
template <typename, int, int>
struct foo
{ };
template <typename, typename, typename = std::tuple<>>
struct NetBase;
// avoid the first couple
template <int ... Is, int J0, int ... Js>
struct NetBase<iList<0, Is...>, iList<J0, Js...>, std::tuple<>>
: NetBase<iList<Is...>, iList<Js...>, std::tuple<>>
{ };
// intermediate case
template <int I0, int ... Is, int J0, int ... Js, typename ... Ts>
struct NetBase<iList<I0, Is...>, iList<J0, Js...>, std::tuple<Ts...>>
: NetBase<iList<Is...>, iList<Js...>,
std::tuple<Ts..., foo<float, I0, J0>>>
{ };
// avoid the last couple and terminate
template <int I0, typename ... Ts>
struct NetBase<iList<I0>, iList<0>, std::tuple<Ts...>>
{ using type = std::tuple<Ts...>; };
template <int F, int S, int... Os>
struct Net : NetBase<iList<0, F, S, Os...>, iList<F, S, Os..., 0>>
{ };
int main()
{
static_assert(std::is_same<
typename Net<784, 16, 16, 10>::type,
std::tuple<foo<float, 784, 16>, foo<float, 16, 16>,
foo<float, 16, 10>>>{}, "!");
}
的方式,行'M(*(我+ +),* I)'調用未定義的行爲達到C++ 14,在C++ 17 –
*無恥的自我推廣*您可能會發現[不明這個回購](https://github.com/liammcinroy/MetaTemplateNeuralNet)有趣 –