1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <cmath>
#include <functional>
using namespace std;
// BEFORE COMPILING THIS SOURCE: COMMENT OUT `outer(arg);' BELOW
//INT
template <typename Type>
void outer(Type t)
{
t.x();
}
void useInt()
{
int arg;
outer(arg);
}
//=
//DOUBLE
void sqrtArg(double &arg)
{
arg = sqrt(arg);
}
template<typename Fun, typename Arg>
void call(Fun fun, Arg arg)
{
fun(arg);
cout << "In call: arg = " << arg << '\n';
}
//=
//MAIN
int main()
{
double value = 3;
call(sqrtArg, value);
cout << "Passed value, returns: " << value << '\n';
call(sqrtArg, ref(value));
cout << "Passed ref(value), returns: " << value << '\n';
}
/*
Displays:
In call: arg = 1.73205
Passed value, returns: 3
In call: arg = 1.73205
Passed ref(value), returns: 1.73205
*/
//=
|