What header file needs to be included for using nullptr in g++?

后端 未结 4 900
借酒劲吻你
借酒劲吻你 2020-12-10 10:58

I am using g++ 4.4.1 and want to use nullptr, but I am not being able to find which header file is required to be included. It does not seem to be keyword eithe

4条回答
  •  鱼传尺愫
    2020-12-10 11:14

    I would recommend not using nullptr as defined above, because it can be dangerous. If you want to use nullptr the following statement should be true.

    sizeof(nullptr) == sizeof(void*) == sizeof(any pointer)
    

    However, sizeof(nullptr) (as defined above) will not comply to this rule. It will actually evaluate to sizeof(bad nullptr) = 1.

    This is a correct implementation.

    #pragma once
    
    namespace std
    {
        //based on SC22/WG21/N2431 = J16/07-0301
        struct nullptr_t
        {
            template operator any * () const
        {
            return 0;
        }
        template operator T any:: * () const
        {
            return 0;
        }
    
    #ifdef _MSC_VER
        struct pad {};
        pad __[sizeof(void*)/sizeof(pad)];
    #else
        char __[sizeof(void*)];
    #endif
    private:
        //  nullptr_t();// {}
        //  nullptr_t(const nullptr_t&);
        //  void operator = (const nullptr_t&);
        void operator &() const;
        template void operator +(any) const
        {
            /*I Love MSVC 2005!*/
        }
        template void operator -(any) const
        {
            /*I Love MSVC 2005!*/
        }
        };
    static const nullptr_t __nullptr = {};
    }
    
    #ifndef nullptr
    #define nullptr std::__nullptr
    #endif
    

提交回复
热议问题