c++11 returning a tuple from function

Im trying to return a tuple from a function. Im not sure what the problem is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
}


and the error i get is:
1
2
3
4
5
6
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 ~  $ 
Try
return std::make_tuple(x*x, sqrt(x));
It has do something with explicit constructor for std::tuple, though I'd be lying if I said I understood it.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
}
ahh, thank you guys...
Topic archived. No new replies allowed.