
#include <tuple> #include <iostream> #include <string> #include <stdexcept> std::tuple<double, char, std::string> get_student(int id) { if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson"); if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten"); if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum"); throw std::invalid_argument("id"); } int main() { auto student0 = get_student(0); std::cout << "ID: 0, " << "GPA: " << std::get<0>(student0) << ", " << "grade: " << std::get<1>(student0) << ", " << "name: " << std::get<2>(student0) << '\n'; double gpa1; char grade1; std::string name1; std::tie(gpa1, grade1, name1) = get_student(1); std::cout << "ID: 1, " << "GPA: " << gpa1 << ", " << "grade: " << grade1 << ", " << "name: " << name1 << '\n'; } 玩的是 c++的元组=_=,参数竟然能通过尖括号来传递=_=
std::get<0>(student0)
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type& get( tuple<Types...>& t ) noexcept; 感觉非常酷
1 lrxiao 2017-07-20 21:46:55 +08:00 non-type template parameter 需要编译期常量(而且基本限定为 integer literal ) |
2 lrxiao 2017-07-20 21:49:07 +08:00 用 lvalue reference 的做 non-type 的...我是没见过.. |
3 codehz 2017-07-20 22:25:49 +08:00 这就觉得酷啦。。你是没看过那些疯狂用模板元编程的。。。 |
4 feelapi 2017-07-20 22:31:44 +08:00 github.com/steemit/steem, 看吧,vs2015 编译不过,/Zm2000 都不行。GCC5.2,Debian 下编译通过,boost 用的登峰造极。 |