W_W
W_W

Reputation: 537

How does the compiler makes this conversion implicitly?

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

Answers (1)

Pubby
Pubby

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

Related Questions