Jus*_*ica 5 c++ g++ operator-overloading visual-c++ default-arguments
我是运算符重载试验new和delete,并注意到MSVC和GCC出现在他们的实施而不同operator delete.请考虑以下代码:
#include <cstddef>
struct CL {
// The bool does nothing, other than making these placement overloads.
void* operator new(size_t s, bool b = true);
void operator delete(void* o, bool b = true);
};
// Functions are simple wrappers for the normal operators.
void* CL::operator new(size_t s, bool b) { return ::operator new(s); }
void CL::operator delete(void* o, bool b) { return ::operator delete(o); }
auto aut = new (false) CL;
Run Code Online (Sandbox Code Playgroud)
此代码将使用GCC(使用Ideone和TutorialsPoint在线编译器进行测试)进行正确编译,但不能使用MSVC(使用MSVS 2010,MSVS 2015 online和Rextester进行测试).
虽然看起来GCC按照人们的预期编译它,但MSVC会发出错误C2831 ; 我检查了Cppreference,但找不到答案; 在默认参数页面没有提及运营商,以及运算符重载&operator delete的页面不提默认参数.同样,SO的C++ FAQ中的重载new和delete输入也没有提到默认参数.
因此,鉴于此,哪些行为(允许默认参数或将其视为错误)符合C++标准?
链接:
除非下面明确说明,否则运算符函数不能具有默认参数(8.3.6).
(C++ 14标准,[over.oper]/8; C++ 03标准中出现相同的句子).
允许使用默认参数的特定情况是函数调用运算符(operator();参见[over.call]/1).在所有其他情况下,他们是被禁止的.