Stream output and implicit void* cast operator function invocation?

1. What is the void * cast operator function and how does it work here It looks something like this: operator void* () const { return fail()? 0 : this; } The question is: why isn’t an operator bool used here?

The answer is: because that allows invalid conversions which may hide bugs. The above is an example of the safe bool idiom. However, this implementation is actually obsolete.

There exist better implementations of this idiom; the article explains them. 2. How is non-null pointer converted to true and null to false This is just how C++ works: any non-null pointer is considered equivalent to true in a conditional.

Now, why does C++ invoke the operator void* here in the first place? Essentially, when C++ sees an object of an unexpected type, it tries to apply one implicit conversion that would make the object type valid in this context. The compiler therefore tries out all available implicit conversions and looks whether the resulting type would be acceptable in this context.

This is happening her: the compiler sees while (cin >> grade). It knows that basic_istream isn’t valid in the context of a while conditional. So it finds that there is an operator void*, and a void* is valid in this context so C++ applies this conversion.

– Nawaz Jun 8 at 11:22 @Nawaz Wasn’t sure, and too lazy to check.?: forbids implicit conversion between incompatible types. Perhaps 0’s type is correctly inferred as basic_istream* here. But either way the cast makes this explicit.

– Konrad Rudolph Jun 8 at 11:26 @Konrad: Yes. 0 is compatible with any type of pointer, due to implicit conversion. That is the reason its often used as NULL pointer of any type.

Std::istream *p = 0;, std::string *s=0;, void* v = 0; all are fine. Anyway, +1 for good answer. – Nawaz Jun 8 at 11:32 @Nawaz These are different situations however, because they are initialisations.

Initialisations can also call implicit conversion operators, and? : cannot do that. – Konrad Rudolph Jun 8 at 11:42 @ konrad this means it returns istream object pointer?

– avinash Jun 8 at 11:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions