Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 替换功能';新运营商&x27;无法声明';内联';[-Werror,-Winline new delete]_C++_Clang_Inline_New Operator - Fatal编程技术网

C++ 替换功能';新运营商&x27;无法声明';内联';[-Werror,-Winline new delete]

C++ 替换功能';新运营商&x27;无法声明';内联';[-Werror,-Winline new delete],c++,clang,inline,new-operator,C++,Clang,Inline,New Operator,我在使用clang时出现了这个错误。 为什么不能内联声明运算符new ./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: exp

我在使用clang时出现了这个错误。 为什么不能内联声明运算符new

./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__))

                  ^

整个程序中分配函数的所有使用都必须兼容。在一个翻译单元中使用
运算符new
分配的内容必须在另一个翻译单元中使用
运算符delete
进行处理。因此,必须在所有地方为程序提供相同的实现,并且分配函数的替换不是本地事务,而是全局选择

因此,与其要求每个TU都包含相同的代码(这将破坏静默、非侵入性替换的目的,违反此目的将极难诊断),不如要求函数具有外部链接,而不是内联


将分配函数视为程序全局状态的一部分。

因为标准如此规定。但为什么标准禁止这样做?