Implicit/explicit conversion

A

al

1. "Implicit conversion happens in three situations: expression, passing in
arguments of functions and returning expression from function." What is
"returning expression from function" here?

2. This is what my learning about conversion in C++:
Generally in C++, casting should be avoided.
Use explicit conversion rather than implicit conversion.
Use static_cast rather than dynamic_cast or reinterpret_cast, especially
when such conversion is safe during coding.
Use dynamic_cast with casting in a class hierarchy.

Is the above understanding correct conceptually?

3. Is the reason that reinterpret_cast should be avoided that it is system
dependent? What is system dependent here regarding reinterpret_cast? Is
there any other reason?

4. "The static_cast is available is available for a conversion that is
portable, well-defined, and invertible." Could someone help me understand
this statement better?

Thanks!
 
V

Victor Bazarov

al said:
1. "Implicit conversion happens in three situations: expression, passing in
arguments of functions and returning expression from function." What is
"returning expression from function" here?

double foo() {
return 42;
}

The expression '42' here is an int. It will be implicitly converted
to double during function return.
2. This is what my learning about conversion in C++:
Generally in C++, casting should be avoided.
Use explicit conversion rather than implicit conversion.
Use static_cast rather than dynamic_cast or reinterpret_cast, especially
when such conversion is safe during coding.
Use dynamic_cast with casting in a class hierarchy.

Is the above understanding correct conceptually?

I cannot claim full understanding why you have "use explicit rather
than implicit" there. 'static_cast' is a compile-time conversion.
In a class hierarchy, derived is converted to base implicitly (often,
if possible, anyway), using 'dynamic_cast' for that is overkill. I
don't know if this is something you expected as an answer.
3. Is the reason that reinterpret_cast should be avoided that it is system
dependent? What is system dependent here regarding reinterpret_cast? Is
there any other reason?

The Standard says that reinterpret_cast is only guaranteed to work for
conversion A->B->A, and everything else may not work (implementation-
defined).
4. "The static_cast is available is available for a conversion that is
portable, well-defined, and invertible." Could someone help me understand
this statement better?

Where did you find it? Is appearance of "is available" twice a typo?

Usually static_cast is used for a conversion T1->T2 if T2->T1 is implicit.
That's "invertible" part. What they mean by well-defined or portable, I
don't know. FWIW, every C++ program is portable unless it uses some kind
of implementation-defined properties (and it still may be portable, just
not guaranteed to be).

Victor
 
E

Ernst Murnleitner

4. "The static_cast is available is available for a conversion that is
Where did you find it? Is appearance of "is available" twice a typo?

I also wondered once. I guess it was in the Stroustroup.

Greetings
Ernst
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top