Why can't nullptr convert to int?

后端 未结 4 2182
不知归路
不知归路 2021-02-07 09:32

Summary: nullptr converts to bool, and bool converts to int, so why doesn\'t nullptr convert to int

4条回答
  •  面向向阳花
    2021-02-07 09:52

    The keyworkd nullptr is introduced in c++11 because multiple definition of the C NULL, and it confuses when overloading a function with int arguments and NULL.

    #define NULL 0
    #define NULL (void*)0
    

    In the bible The C++ Programming Language (4th), page 270

    The pointer-to-bool conversion is useful in conditions, but confusing elsewhere.

    So I think nullptr_t varible convert to int is not allowed because that's the reason why it exists, but it can be used as a test condition like bool variables.

提交回复
热议问题