分配给std :: tie和引用的元组有什么区别? [英] What is the difference between assigning to std::tie and tuple of references?

查看:1109
本文介绍了分配给std :: tie和引用的元组有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下元组业务感到困惑:

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

使用 std :: tie 但直接赋值给引用的元组不会编译,给出


错误:从'std :: tuple转换>'to non-scalar type'std :: tuple< int& amp;>'requested





没有合适的用户定义转换从std :: tuple< int,float>到std :: tuple< int& p>

为什么?我用编译器双重检查,如果它是通过这样做分配的相同类型:

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

此评估为true。



也在在线检查是否可能是msvc的故障,但所有编译器给出相同的结果。


std :: tie()函数实际上初始化的成员 std :: tuple< T& ...> 其中 std :: tuple< T& ...> 不能由模板 std :: tuple< T ...> 初始化。操作 std :: tie()并且初始化相应的对象将被表示如下:

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

(显然,通常使用不同于已绑定变量的值)。


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.

解决方案

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).

这篇关于分配给std :: tie和引用的元组有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆