c++ - 模板参数推导与结构化绑定不同
问题描述
我正式被告知(一些书籍或网络),类型推导auto
与参数类型推导相同template
。
但是,下面的代码无法成功编译,因为它会引发increment of read-only reference a
错误。虽然模板版本扣除T
为 typeint
而不是const int &
. 为什么这两种情况有不同的扣除结果?
#include "bits/stdc++.h"
using namespace std;
template <typename T>
T minmax2(T n1, T n2) {
n1++;
return n2;
}
int main() {
auto [a, b] = minmax(1, 2); // minmax() return const T &
a++; // compile error: can not alter read-only varaible.
const int & c = 1, & d = 2;
minmax2(c, d); // the same parameter type, don't raise error
}
另一个例子:
int tmp = 1;
const int & test() {
return tmp;
}
template <typename T>
void test1(T n) {
n++; // no error
}
int main() {
auto a = test(); // If the satement is true, I expect this is the same as test1.
a++; // no compile error, which is different with the previous code exp
const int & b = 2;
test1(b);
}
解决方案
我认为您的困惑来自这样一个事实,即结构化绑定的行为与普通绑定有点不同auto
,至少从用户的角度来看是这样。先考虑后者:
const int& i = 0;
auto j = i;
std::cout << &i << std::endl;
std::cout << &j << std::endl; // different address
j++; // compiles
在这里,i
andj
指的是不同的对象和类型j
是int
(不是const int
或const int&
)。这与按值传递参数时的模板参数推导完全对应。
但是对于结构化绑定,情况就不同了:
int c = 0, d = 0;
auto [a, b] = std::pair<const int&, const int&>(c, d);
std::cout << &a << std::endl;
std::cout << &c << std::endl; // same address
// a++; // would not compile
在这里,and 的类型a
是b
和const int&
两者都a
指c
的是同一个对象。
现场演示:https ://godbolt.org/z/Wq1zsz
实际上,结构化绑定也忽略了初始化表达式的const和引用部分,但在“外部”级别,即std::pair
在我们的例子中:
const auto& p = std::make_pair(c, d);
auto [x, y] = p;
std::cout << &p.first << std::endl;
std::cout << &x << std::endl; // different address
这里,x
是一个类型为 的新对象int
。如果我们const auto& [x, y] = p;
改为使用 then ,则x
引用p.first
其类型为const int&
.
推荐阅读
- spring - 春季启动注解@springbootapplication
- rust - 为什么 Command::output 阻塞的时间比它产生的子进程长?
- javascript - 在数百万条记录中搜索的最快方法是两个 js 对象的组合
- python - mysql.connector 在尝试连接到本地主机上的数据库时返回错误
- jquery - reactjs youtube视频自动播放可以在没有静音的情况下实现吗?
- amazon-web-services - AWS SSM 未被识别为命令
- asciidoc - 无法使用 asciidoctor-pdf 设置字体大小
- javascript - HtmlWebpackPlugin 未添加字符集元标记
- git - 无法将文件添加到 Git 存储库
- java - 如何仅打印数组中具有重复字符的单词?