为什么预增量工作但后增量不在参考变量上?
#include <iostream>
void swap(int&, int&);
int main()
{
int x=10, y=20;
int &a=x, &b=y;
swap(++a, ++b); //swap (a++,b++) is not allowed.
printf("%d %d ", a, b);
return 0;
}
void swap(int& x, int& y)
{
x+=2;
y+=3;
}
Run Code Online (Sandbox Code Playgroud)
为什么swap(++a, ++b)允许,但swap(a++, b++)说:
[Error]从'int'类型的右值初始化'int&'类型的非const引用无效
c++ ×1