为什么C++运算符new/delete/variants不应该在头文件中?

Sto*_*row 12 c++ gcc delete-operator translation-unit

有人可以解释这个C++编译错误的本质吗?我正在涉及/学习重载全局运算符new,delete及其变体.我读了一对夫妇 文章 主题,但我无法找到一个似乎专门解决这个问题.

代码

foo.h:

#ifndef foo_h
#define foo_h

void* operator new(size_t);
void* operator new[](size_t);

void operator delete(void*);
void operator delete[](void*);

#endif // foo_h
Run Code Online (Sandbox Code Playgroud)

foo.cpp:

#include <foo.h>
#include <iostream>

void* operator new(size_t size) { return NULL; }
void* operator new[](size_t size) { return NULL; }

void operator delete(void* p) { }
void operator delete[](void* p) { }
Run Code Online (Sandbox Code Playgroud)

编译错误

>g++ -g -std=c++14 -I./ -c foo.cpp -o foo.o
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ext/new_allocator.h:33:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/x86_64-pc-cygwin/bits/c++allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/allocator.h:46,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/string:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/iostream:39,
                 from foo.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/new:116:41: error: declaration of ‘void operator delete(void*) noexcept’ has a different exception specifier
   __attribute__((__externally_visible__));
                                         ^
In file included from foo.cpp:1:0:
./foo.h:8:6: error: from previous declaration ‘void operator delete(void*)’
 void operator delete(void* p);
      ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ext/new_allocator.h:33:0,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/x86_64-pc-cygwin/bits/c++allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/allocator.h:46,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/string:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/iostream:39,
                 from foo.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/new:118:41: error: declaration of ‘void operator delete [](void*) noexcept’ has a different exception specifier
   __attribute__((__externally_visible__));
                                         ^
In file included from foo.cpp:1:0:
./foo.h:9:6: error: from previous declaration ‘void operator delete [](void*)’
 void operator delete[](void* p);
      ^
Run Code Online (Sandbox Code Playgroud)

关于这个问题的一些奇怪之处我认为是相关的:

  • 如果我注释掉#include <iostream>foo.cpp,编译成功
  • 如果我将函数声明注释掉foo.h,并且只保留它们的定义foo.cpp(并保持#include <iostream>),则编译成功.

我有一些模糊的怀疑; 也许回答者会通过他们的回答确认:

  • 错误提到了一个exception specifier所以我想也许通过覆盖任何这些运算符,我不得不覆盖他们兄弟姐妹的整个套件.但是,添加operator delete(void*, const std::nothrow_t&)声明和定义不会更改编译错误.我也不认为压倒任何这些操作符都要求编码人员实施所有操作符,但我错了吗?
  • 我在StackOverflow之外阅读了一篇文章,提到这些运算符只能包含在一个"翻译单元"中,因此不应该在头文件中.我不明白翻译单元是什么,那篇文章没有解释它是什么.如果这与此问题有关,请解释"翻译单元"是什么以及为什么需要从头文件中排除函数声明 - 这似乎与我以前的所有C++编码经验相反.

感谢您的任何见解.

R S*_*ahu 5

您看到的问题是由于以下声明中的差异.

该库将operator delete函数声明为:

void operator delete(void*) noexcept;
void operator delete [](void*) noexcept;
Run Code Online (Sandbox Code Playgroud)

当你将它们声明为:

void operator delete(void*);
void operator delete [](void*);
Run Code Online (Sandbox Code Playgroud)

您应该使用而不是在.h文件中声明它们

#include <new>
Run Code Online (Sandbox Code Playgroud)

查找部分18.6 C++ 11标准的动态内存管理,如果您有权访问它,可以获得有关该主题的更多信息.

翻译单元通常是.cpp文件.进一步阅读:什么是C++中的"翻译单元".