The replacement function "operator new" cannot be declared "inline" [-Werror, -Winline-new-delete]

I got this error while using clang. Why can't the new operator be declared inline?

./test.h:198:1: error: replacement function 'operator new' cannot be declared 'inline' [-Werror,-Winline-new-delete]
__forceinline void *operator new(size_t size) { return malloc(size); }
^
./test.h:18:23: note: expanded from macro '__forceinline'
#define __forceinline inline __attribute__((__always_inline__))

                  ^

      

+3


source to share


1 answer


All distribution functions in your program must be compatible. What is allocated operator new

in one translation unit must be released with the help operator delete

in another TU. Thus, the program must have the same implementations everywhere, and the replacement of distribution functions is not a local matter for TU, but a global choice.

So instead of requiring each TU to include the same code (which would defeat the goal of tacit, non-invasive substitution, and which would be extremely difficult to diagnose to break), the requirement is instead that the function is externally connected and not built-in.



Think of distribution functions as part of the global state of your program.

+3


source







All Articles