Created
November 19, 2015 04:15
-
-
Save chenfengyuan/8ccf72960dfdf059f42d to your computer and use it in GitHub Desktop.
wrong way of using std::max
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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