聊聊std::tie
tie既能用来解包有又能用来赋值。这是怎么做到的
1. 例子
1
2
template< class... Types >
tuple<Types&...> tie( Types&... args ) noexcept;
直接看例子
- 比较自定义结构体
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <tuple>
struct person {
std::string first_name;
std::string last_name;
int age;
};
inline auto tie_members(const person & x) noexcept {
return std::tie(x.first_name, x.last_name, x.age);
}
inline bool operator<(const person & lhs, const person & rhs) noexcept {
return tie_members(lhs) < tie_members(rhs);
}
- 返回值解包
1
2
bool was_inserted;
std::tie(std::ignore, was_inserted) = some_set.insert(some_value)
2. 原理
std::tie实际上是引用形态的std::tuple
1
2
template< class... UTypes >
tuple& operator=( const tuple<UTypes...>& other );
具体的解一个tuple其实也是可以的
1
2
3
int a;
std::tie(a) = std::make_tuple(24);
return a; // 24
实际上这个code可以转化成
1
2
3
int a;
std::tuple<int&>{a} = std::tuple<int>{24};
return a; // 24
具体的结构体可以是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct T { // substituent for std::tuple<int>
int x;
};
struct Tr { // substituent for std::tuple<int&>
int& xr;
auto operator=(const T& other)
{
// std::get<I>(*this) = std::get<I>(other);
xr = other.x;
}
};
auto foo()
{
int a;
Tr{a} = T{24};
return a; // 24
}
所以实际上他们很类似做了一个转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
auto foo()
{
int a;
{ // block substituent for temporary variables
// Tr{a}
int& tr_xr = a;
// T{24}
int t_x = 24;
// = (asignement)
tr_xr = t_x;
}
return a; // 24
}
REF
This post is licensed under CC BY 4.0 by the author.