是否可以将 operator new 重载为 constexpr 函数?就像是:
constexpr void * operator new( std::size_t count );
原因是在重载的运算符主体内执行 constexpr 函数,其中 count 参数值将是输入数据......因为运算符被调用:
SomeClass * foo = new SomeClass();
数据类型的大小在编译时就知道了,不是吗?( count== sizeof(SomeClass)
) 那么计数可以被认为是编译时间常数吗?
constexpr void * operator new( std::size_t count )
{
if constexpr ( count >= 10 ) { /* do some compile-time business */ }
}
非常感谢任何愿意提供帮助的人!