Skip to content

Instantly share code, notes, and snippets.

@chenfengyuan
Created November 19, 2015 04:15
Show Gist options
  • Save chenfengyuan/8ccf72960dfdf059f42d to your computer and use it in GitHub Desktop.
Save chenfengyuan/8ccf72960dfdf059f42d to your computer and use it in GitHub Desktop.
wrong way of using std::max
#include <algorithm>
#include <iostream>
struct C{
int v{};
C(int v_):v{v_}{
std::cout << "C()[" << v << "]\n";
}
C(const C &)=delete;
bool operator<(C const & o) const{
return v < o.v;
}
C & operator=(const C &)=delete;
~C(){
std::cout << "~C()[" << v << "]\n";
v = 42;
}
};
C foo(){
return {2};
}
int main(){
C a{1};
C const & c = std::max(a, foo());
std::cout << c.v << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment