Reputation: 537
I was watching a video from //build, where Herb Sutter showed the benefit of explicit conversion keyword with a code snippet:
template< /* ... */ > class unique_ptr {
public:
// ...
explicit operator bool() const { return get() != nullptr; }
And he said with that keyword, we can prevent this to compile:
use(ptr * 42); // oops, meant *(ptr) * 42
I really cannot get it, how does the showcase get compiled? How does compiler make the conversion? To what type?
Upvotes: 2
Views: 78
Reputation: 53047
It's implicitly converting from unique_ptr
to bool, and then from bool to int to do the multiplication.
(bool to int means true is 1 and false is 0)
Upvotes: 1