simont |
Sat 2019-03-09 21:34 |
Apparently you know more about std::forward than I do! I only know of it as the mystic rune you copy and paste when you want to pass the argument list of one function straight to another (usually in conjunction with a template parameter pack); I've never got my head round exactly what it does in detail. I suppose that does put it in precisely this kind of headspace of 'preserve whatever lvalueosity you had as input', but I certainly couldn't have come up with this more sophisticated use of it. That said, hmmm. This technique lets me give two variables as the arguments, or two integer literals, but something still goes wrong if I give it one of each: $ cat one-of-each.cpp
#include <utility>
template<typename T>
T&& rvmax(T&& u, T&& v) { return std::forward<T>(u>v ? u : v); }
int f(int x) { return rvmax(x, 1000); }
$ g++ -c one-of-each.cpp
one-of-each.cpp: In function ‘int f(int)’:
one-of-each.cpp:6:36: error: no matching function for call to ‘rvmax(int&, int)’
int f(int x) { return rvmax(x, 1000); }
^
one-of-each.cpp:4:5: note: candidate: template<class T> T&& rvmax(T&&, T&&)
T&& rvmax(T&& u, T&& v) { return std::forward<T>(u>v ? u : v); }
^~~~~
one-of-each.cpp:4:5: note: template argument deduction/substitution failed:
one-of-each.cpp:6:36: note: deduced conflicting types for parameter ‘T’ (‘int&’ and ‘int’)
int f(int x) { return rvmax(x, 1000); }
^ |
|