重载算子新功能的限制
Restrictions in overloading operator new function
运算符新函数可以被重载,但它要求第一个参数是无符号的int,即重载的函数也应该将无符号的int作为其第一个参数。
// Okay since the first argument is unsigned int
void * operator new(size_t size, Arena& arena, std::string msg)
// Not okay. since the first argument is not an unsigned int
void * operator new(Arena& arena, std::string msg)
如何实施? 我们可以对用户定义的函数实施类似的限制吗?
在 C++ 中使用运算符重载时,运算符签名由 C++ 标准确定。该标准更进一步,提到了一些不允许超载的操作者(?:
、::
等(。
我不是一个从C++标准中提取东西的语言律师,但正如 cpp-reference 中所述,与任何其他运算符一样,new operator
都有公认的预定义函数签名。使用示例中所示的函数签名,似乎定义了用户定义的放置分配函数。根据 cpp-reference,第一个参数应始终是该特定运算符新重载size_t
类型。
附言这是operator new
不operator new function
.
相关文章:
- c++20[[no.unique_address]]中的新功能是什么
- 如果我想在没有更新编译器的情况下使用新功能,该怎么办?
- 重载算子新功能的限制
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 如何组织 git 工作流以修复错误并同时引入新功能
- 新功能"synchronized"块在C++中提供了什么优势?
- C++17 'inline variable' vs 'extern' 关键字继承自 C 的新功能
- 如何制作新功能并将其传递到另一个功能
- 我不能在函数中使用新功能C++构建
- 是否可以保证C++标准库容器调用可替换的新功能
- 全局新运算符重载
- 向类添加新功能
- 具有新功能的 C++ 动态分配
- 在映射中插入地址时,新运算符重载会导致无限递归
- 避免每次要引入新功能时都使用新类
- 继承构造函数并提供新的重载:无参数基构造函数似乎不参与重载解析
- Linux 中的新功能后没有变化
- 从类级别放置内访问成员 新的重载
- C++11 是否支持 C11 的新功能?
- C++中的新功能语法