As I have been taught it, the code of an inline function is expanded into its call during compilation. Thus, for example, given the definition
Code:
inline int max{
return x > y ? x : y;
}
a line
should expand into
Code:
int a = b > c ? b : c;
But now say I have a program with an inline function like the following which is supposed to change one of its arguments:
Code:
#include<iostream>
using namespace std;
inline void set(int x, int y){
x = y;
}
int main(){
int a = 3, b = 5;
cout << a << " " << b << endl;
set(a,b); //I would think this is replaced by a = b;
cout << a << " " << b << endl;
return 0;
}
The output of this is the same for both lines: a = 3, and b = 5, and thus a is not changed to 5, as I would initially think would happen with an inline function. Of course, with a non-inline function, set(a,b) passes by value, so a and b should stay unchanged. (Oddly enough, I have successfully been able to change the variable a if I write set(a,b) as a parameterized macro.) But how do I resolve these two conflicting ideas I have, that (1) inline functions expand the function code whenever the function is called; (2) inline functions seem to pass by value, like regular functions?