Advertisement
Guest User

Untitled

a guest
Jul 6th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. //size_t I = 0 means the default value of I is 0(index of tuple is zero)
  2. template<size_t I = 0, typename DataType, typename... Tp>
  3. void Forward(const DataType& input, std::tuple<Tp...>& network)
  4. {
  5.     //set the value of InputParameter of network
  6.     std::get<I>(network).InputParameter() = input;
  7.  
  8.     //call the forward function of network
  9.     std::get<I>(network).Forward(std::get<I>(network).InputParameter(),
  10.                            std::get<I>(network).OutputParameter());
  11.  
  12.     //now, we start the recursive, increment the I by 1
  13.     ForwardTail<I + 1, Tp...>(network);
  14. }
  15.  
  16. //size_t I = 1 means the default value of I is 0(index of tuple is one)
  17. //we use std::enable_if to specify the case when the sizeof the tuple
  18. //are same as I. In short, std::enable_if<I==sizeof...(Tp), void>::type
  19. //tell the compiler---when the I == sizeof tuple(network), you should
  20. //call(instantiate) this function, else call another(if exist).
  21. //This function it the end condition of the Forawrd too
  22. template<size_t I = 1, typename... Tp>
  23. typename std::enable_if<I == sizeof...(Tp), void>::type
  24. ForwardTail(std::tuple<Tp...>& network)
  25. {
  26.     LinkParameter(network);
  27. }
  28.  
  29.  
  30. //This function only instantiate if I < sizeof tuple(network)
  31. //We use std::enable_if to specialize(do some "overload" at compile time)
  32. //the function
  33. template<size_t I = 1, typename... Tp>
  34. typename std::enable_if<I < sizeof...(Tp), void>::type
  35. ForwardTail(std::tuple<Tp...>& network)
  36. {
  37.     std::get<I>(network).Forward(std::get<I - 1>(network).OutputParameter(),
  38.         std::get<I>(network).OutputParameter());
  39.  
  40.     //recursive call, this time we add the index by 1
  41.     ForwardTail<I + 1, Tp...>(network);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement