123456789101112131415
#include <iostream> #include <tuple> #include <cmath> std::tuple<int, double> square_and_sqrt(int x){ return {pow(x, 2), sqrt(x)}; } int main(){ auto t = square_and_sqrt(9); std::cout << std::get<0>(t) << std::get<1>(t) << std::endl; }
123456
metulburr@ubuntu ~ $ g++ -std=c++11 -o test -c test.cpp test.cpp: In function ‘std::tuple<int, double> square_and_sqrt(int)’: test.cpp:6:31: error: converting to ‘std::tuple<int, double>’ from initializer list would use explicit constructor ‘constexpr std::tuple<_T1, _T2>::tuple(_U1&&, _U2&&) [with _U1 = double; _U2 = double; <template-parameter-2-3> = void; _T1 = int; _T2 = double]’ return {pow(x, 2), sqrt(x)}; ^ metulburr@ubuntu ~ $
return std::make_tuple(x*x, sqrt(x));
#include <iostream> #include <tuple> #include <cmath> std::tuple<int, double> square_and_sqrt(int x){ return std::make_tuple(pow(x, 2), sqrt(x)); } int main(){ auto t = square_and_sqrt(9); std::cout << std::get<0>(t) << std::get<1>(t) << std::endl; }