I have the same function with the only difference that it will either increment or decrement. I would like to generalize from that.
template<typename O>
void f(int& i, O op){
op(i);
}
int main() {
int i;
f(i,operator++);
f(i,operator--);
return 0;
}
How can I make this work?
my other option is to use functional std::plus or have two functions but I'd prefer this solution if possible. Thank you.
operator++
, there would be no need forstd::plus
...) – Niobicf(i,[](int & v) { ++v; });
– Oppenf(i, x => x++);
– Strawboards/int/auto/
, and might also want to return something :) – Niobicauto
does not compile in C++11 because it does not support generic lambdas. (I do not use C++14 in any examples until it is no longer a draft.) And why would I return anything in this case? The return value ofop(i)
is not used, so it's pointless to do so. – Oppenoperator++
directly without hacks? – Rhigolenestd::plus
and modified the code to bei=op(i);
. that or lambda is a matter of taste. – Rhigolene