0

cppreference explains that one possible const_cast conversion is a "null pointer value may be converted to the null pointer value of new_type".

Is there any other way const_cast can return a null pointer?

jesses
  • 559
  • 3
  • 15
  • [expr.const.cast](http://eel.is/c++draft/expr.const.cast) lists all the cases, it is easy to see that the answer is no. – n. m. could be an AI Oct 02 '20 at 08:34
  • 1
    note that here C++ (surprisingly) misses an opportunity to allow you to shoot yourself into your foot. You cannot provide a custom conversion operator from `T` to `const T` or vice versa – 463035818_is_not_an_ai Oct 02 '20 at 08:36
  • 2
    @idclev463035818: There's always time to slip it into C++23. – Bathsheba Oct 02 '20 at 08:37
  • 1
    seems like you either you encountered some problem in code that you should have included in the question or this is a rather academical question (no problem about that), if it is the first, better ask about your actual problem – 463035818_is_not_an_ai Oct 02 '20 at 08:38
  • @idclev463035818 it's mostly academical. I do have to maintain some code and found a place where someone felt like checking the result of the const_cast for nullptr and i couldn't find any reason for it but also no explicit mention of this anywhere. I'd tend to conclude the same as n.'pronouns'm. but was wondering why i couldn't find a similar question anywhere. Maybe it's too obvious. – jesses Oct 02 '20 at 16:18

0 Answers0