简体   繁体   中英

What is the difference between assigning to std::tie and tuple of references?

I am a bit puzzled by the following tuple business:

int testint = 1;
float testfloat = .1f;
std::tie( testint, testfloat ) = std::make_tuple( testint, testfloat );
std::tuple<int&, float&> test = std::make_tuple( testint, testfloat );

With std::tie it works, but assigning directly to the tuple of references doesn't compile, giving

"error: conversion from 'std::tuple<int, float>' to non-scalar type 'std::tuple<int&, float&>' requested"

or

"no suitable user-defined conversion from std::tuple<int, float> to std::tuple<int&, float&>"

Why? I double checked with the compiler if it's really the same type that is being assigned to by doing this:

static_assert( std::is_same<decltype( std::tie( testint, testfloat ) ), std::tuple<int&, float&>>::value, "??" );

Which evaluates as true.

I also checked online to see if it maybe was the fault of msvc, but all compilers give the same result.

Both make_tuple and tie will deduce the returned type by arguments. But tie will make a lvalue reference type according to deduced type and make_tuple will make an actual tuple.

std::tuple<int&, float&> a = std::tie( testint, testfloat );

std::tuple<int , float > b = std::make_tuple( testint, testfloat );

The goal of tie is making a temporary tuple to avoid temporary copies of tied objects, the bad effect is, you can not return a tie if entry objects are local temporary.

The std::tie() function actually initializes the members of the std::tuple<T&...> of references where is the std::tuple<T&...> can't be initialized by a templatory std::tuple<T...> . The operation std::tie() does and initializing a corresponding object would be expressed like this:

std::tuple<int&, float&> test = 
    std::tuple<int&, float&>(testint, testfloat) = std::make_tuple(testint, testfloat);

(obviously, you would normally use different values than those of the already bound variables).

The problem is that the rhs std::make_tuple(testint, testfloat) doesn't return an array of references, it returns std::tuple<int, int> , which is a temporary whose values can't bind to lvalue-references. If you need a tuple of references, you can use the helper function std::ref :

auto test = std::make_tuple(std::ref(a), std::ref(b));
//                          ^^^^^^^^^^^  ^^^^^^^^^^^

The difference between this and tie is that the references are initialized by std::tie(a, b) on construction.

I guess, because they are different types and there is no conversion from one to another, but there is a templated copy assignment operator, that works in case of a tie.

Checking the code

#include <tuple>
#include <iostream>

int main() {

    std::tuple<int> a{};

    std::cout << std::get<0>(a) << std::endl;

    std::tuple<float> b{1.f}; //note float type

    a = b;

    std::cout << std::get<0>(a) << std::endl;

}
output: 0 1

suggests, that it's probably correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM