Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <tuple>
- #include <type_traits>
- //we use std::enable_if to specify the end condition
- template<size_t I, typename... Tp>
- typename std::enable_if<I == sizeof...(Tp), void>::type
- print_tuple(std::tuple<Tp...> const&)
- {
- }
- //This is one of the solution to write
- //compile time loop in c++
- //std::enable_if work because c++ follow the rule of
- //"SFINAE"(you can check it if you are interesting about it)
- //the second parameter of enable_if is the type itself
- template<size_t I = 0, typename... Tp>
- typename std::enable_if<I < sizeof...(Tp), void>::type
- print_tuple(std::tuple<Tp...> const &tuples)
- {
- std::cout<<std::get<I>(tuples)<<std::endl;
- print_tuple<I+1>(tuples);
- }
- int main()
- {
- //exactly, when you specify your types by tuples like this(by template)
- //the compiler already know your type at compile time
- //Maybe we can said that, everything related to template must be known at
- //compile time, it is a pure compile time infrastructure
- std::tuple<std::string, int, float, double> const tuples(
- "one", 2, 3.0f, 4.0);
- print_tuple(tuples);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement