#include <iostream>
using namespace std;
struct A
{
explicit operator bool() const
{
return true;
}
operator int()
{
return 0;
}
};
int main()
{
if (A())
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
}
La mia aspettativa era che A()
sarebbe stato contestualmente convertito in bool
usando il mio operator bool()
, e quindi stampare true
.Perché il mio "operatore esplicito bool()" non è chiamato?
Tuttavia, l'output è false
, che indica che è stato invocato invece operator int()
.
Perché il mio explicit operator bool
non viene chiamato come previsto?
Mi aspetto che l'output sia vero. Tuttavia, l'output reale è 'false' piuttosto che' true'! – xmllmx
Per favore chiarisci la tua domanda. – juanchopanza
@xmllmx Non hai elaborato nulla. –