Is it possible to completely disable the default C++ new operator?

前端 未结 4 1237
走了就别回头了
走了就别回头了 2021-02-01 14:03

Because our app has hard performance and memory constraints, our coding standards forbid the use of the default heap — ie, no malloc, no default new

4条回答
  •  清酒与你
    2021-02-01 14:43

    You can just declare operator new = delete, similar to how you'd disable some constructors. Like

    struct Foo {
        void* operator new(std::size_t) = delete;
        void* operator new[](std::size_t) = delete;
    };
    

    That'll give you compiler errors when you try to use new with this class. See https://godbolt.org/z/RToOcf

提交回复
热议问题