gpt4 book ai didi

c++ - 赋值表达式是左值引用吗?

转载 作者:太空狗 更新时间:2023-10-29 20:23:35 24 4
gpt4 key购买 nike

例如:

template <typename T> void g(T &&val);
int i = 0; const int ci = i;
g(i = ci);

g 的模板参数是什么?

最佳答案

根据 §5.18/1:

The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand. [...]

所以,在

g(i = ci)

左操作数 i 被返回,因此 T 被推导为 int&

您可以通过以下代码段进行检查:

#include <type_traits>

template <typename T>
void g(T &&val) {
static_assert(std::is_same<T, int&>::value, "Nope");
}

int main() {
int i = 0; const int ci = i;
g(i = ci);
}

Live demo

关于c++ - 赋值表达式是左值引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32414448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com