在我看来,你需要整数的两个列表,出来的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)有趣 –