Skip to content

Instantly share code, notes, and snippets.

@zeddie888
Created December 10, 2024 06:52
Show Gist options
  • Select an option

  • Save zeddie888/4ef7f3a824dfc442f443e7801b877773 to your computer and use it in GitHub Desktop.

Select an option

Save zeddie888/4ef7f3a824dfc442f443e7801b877773 to your computer and use it in GitHub Desktop.
Variadic min + return a reference to the min element so we can mess with it directly
#include <iostream>
using namespace std;
template <typename T>
T& min(T& value) {
return value;
}
template <typename T, typename... Args>
T& min(T& first, Args&... rest) {
T& min_rest = min(rest...);
return (min_rest < first) ? min_rest : first;
}
int main() {
int a = 2, b = 5, c = 1;
int& res = min(a, b, c);
res = 333;
cout << a << ", " << b << ", " << c << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment