Home > Software engineering >  How is std::vector<bool>::reference assigned to bool type in c ?
How is std::vector<bool>::reference assigned to bool type in c ?

Time:11-04

I know std::vector<bool>::reference is a proxy class that is not apparent to users.

It is implicitly converted to bool when assigned to bool type.

How is it possible? type std::vector<bool>::reference is far far from the type bool.

Is there some compiler work under the hood?

Below is the code example

...
    std::vector<bool> v = { true, true, false, false, false };
    std::vector<bool>::reference vr =  v[1];
    bool b = vr; // How is it possible ?
    std::cout << b << std::endl;
...

CodePudding user response:

std::vector<bool>::reference defines operator bool to allow implicit conversion to bool.

  • Related