Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualCornstalks

Posted 26 February 2013 - 12:09 PM

Am I missing something here?

It's entirely possible I missed something in my first post and I'll have to embarrassingly revoke it, but my understanding is that in this example, std::tie results in:
std::tuple<std::array<int, 2>&, std::array<int, 2>&> // references!
 
However, that's not what the function returns. The function returns:
std::tuple<std::array<int, 2>, std::array<int, 2>>
 
So a copy must be made, much like if you did:
int foo()
{
    int x = 2;
    int& y = x;
 
    return y; // still safe, despite y being a reference, because the value gets copied
}
 
Indeed, we can try:
 
#include <typeinfo>
#include <iostream>
#include <tuple>
#include <array>
 
std::tuple<std::array<int, 2>, std::array<int, 2>> foo()
{
   std::array<int, 2> buff = {1, 2};
   std::array<int, 2> buff2 = {1, 2};
 
   std::cout << &buff << '\t' << buff.data() << std::endl;
   std::cout << &buff2 << '\t' << buff2.data() << std::endl;
 
   return std::tie(buff, buff2);
}
 
int main()
{
    auto t = foo();
 
    std::cout << &t << std::endl;
    std::cout << &std::get<0>(t) << '\t' << &std::get<1>(t) << std::endl;
    std::cout << std::get<0>(t).data() << '\t' << std::get<1>(t).data() << std::endl;
}
 
Running this, I got:

0x7fff54c9a7f8    0x7fff54c9a7f8
0x7fff54c9a7f0    0x7fff54c9a7f0
0x7fff54c9ab08
0x7fff54c9ab08    0x7fff54c9ab10
0x7fff54c9ab08    0x7fff54c9ab10
 
As you can see, the addresses in foo() aren't the same addresses as for t. At least as far as I've understood this so far.

#2Cornstalks

Posted 26 February 2013 - 12:07 PM

Am I missing something here?

It's entirely possible I missed something in my first post and I'll have to embarrassingly revoke it, but my understanding is that in this example, std::tie results in:
std::tuple<std::array<int, 2>&, std::array<int, 2>&> // references!
 
However, that's not what the function returns. The function returns:
std::tuple<std::array<int, 2>, std::array<int, 2>>
 
So a copy must be made, much like if you did:
int foo()
{
    int x = 2;
    int& y = x;
 
    return y; // still safe, despite y being a reference, because the value gets copied
}
 
Indeed, we can try:
 
#include <typeinfo>
#include <iostream>
#include <tuple>
#include <array>
 
std::tuple<std::array<int, 2>, std::array<int, 2>> foo()
{
   std::array<int, 2> buff = {1, 2};
   std::array<int, 2> buff2 = {1, 2};
 
   std::cout << &buff << '\t' << buff.data() << std::endl;
   std::cout << &buff2 << '\t' << buff2.data() << std::endl;
 
   return std::tie(buff, buff2);
}
 
int main()
{
    auto t = foo();
 
    std::cout << &t << std::endl;
    std::cout << &std::get<0>(t) << '\t' << &std::get<1>(t) << std::endl;
    std::cout << std::get<0>(t).data() << '\t' << std::get<1>(t).data() << std::endl;
}
 
Running this, I got:

0x7fff54c9a7f8    0x7fff54c9a7f8
0x7fff54c9a7f0    0x7fff54c9a7f0
0x7fff54c9ab08
0x7fff54c9ab08    0x7fff54c9ab10
0x7fff54c9ab08    0x7fff54c9ab10
 
As you can see, the addresses in foo() aren't the same addresses as for t.

#1Cornstalks

Posted 26 February 2013 - 12:06 PM

Am I missing something here?

It's entirely possible I missed something in my first post and I'll have to embarrassingly revoke it, but my understanding is that in this example, std::tie results in:

std::tuple<std::array<int, 2>&, std::array<int, 2>&> // references!

 

However, that's not what the function returns. The function returns

std::tuple<std::array<int, 2>, std::array<int, 2>>

 

So a copy must be made, much like if you did:

int foo()
{
    int x = 2;
    int& y = x;
 
    return y; // still safe, despite y being a reference, because the value gets copied
}

 

Indeed, we can try:

 
#include <typeinfo>
#include <iostream>
#include <tuple>
#include <array>
 
std::tuple<std::array<int, 2>, std::array<int, 2>> foo()
{
   std::array<int, 2> buff = {1, 2};
   std::array<int, 2> buff2 = {1, 2};
 
   std::cout << &buff << '\t' << buff.data() << std::endl;
   std::cout << &buff2 << '\t' << buff2.data() << std::endl;
 
   return std::tie(buff, buff2);
}
 
int main()
{
    auto t = foo();
 
    std::cout << &t << std::endl;
    std::cout << &std::get<0>(t) << '\t' << &std::get<1>(t) << std::endl;
    std::cout << std::get<0>(t).data() << '\t' << std::get<1>(t).data() << std::endl;
}

PARTNERS