ave(la, lb); 是不是将 template <typename type1> type1 swap(type1 a, type1 b) 实例成 swap<int>( int, int ) ?
所以
程序代码:
#include <iostream>
using namespace std;
template<typename type1>
void ave( type1 a, type1 b )
{
a = b;
}
int main()
{
int a = 0;
int b = 1;
int& la = a;
int& lb = b;
ave(la, lb);
cout << a << " " << b;
}
等同于
程序代码:
#include <iostream>
using namespace std;
void ave( int a, int b )
{
a = b;
}
int main()
{
int a = 0;
int b = 1;
int& la = a;
int& lb = b;
ave(la, lb);
cout << a << " " << b;
}
展开函数调用,就等同于
程序代码:
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 1;
int& la = a;
int& lb = b;
int ave_a = la;
int ave_b = lb;
ave_a = ave_b;
cout << a << " " << b;
}
里面没有一句在修改a