crazyscot: Selfie, with C, in front of an alpine lake (Default)
crazyscot ([personal profile] crazyscot) wrote in [personal profile] simont 2019-03-09 08:33 pm (UTC)

(edit: posted correct version of my scratch code)

Isn't this what rvalue references were made for?


#include <iostream>
#include <utility>

template<typename T>
T&& rvmax(T&& u, T&& v) { return std::forward<T>(u>v ? u : v); }

int main()
{
int var1 = 100;
int var2 = 9;
std::cout << "before: " << var1 << ',' << var2 << std::endl;
rvmax(var1,var2) = (var1-var2) / 2;
std::cout << "after: " << var1 << ',' << var2 << std::endl;
std::cout << "rvalue max: " << rvmax(100,9) << std::endl;
}


$./a.out
before: 100,9
after: 45,9
rvalue max: 100

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting