草庐IT

hljs-operator

全部标签

c++ - 将 operator new 和 operator delete 与自定义内存池/分配器一起使用

我正在研究一个内存池/内存分配器实现,我正在一个庄园中设置它,只有一个特殊的“客户端”对象类型可以从池中提取。客户端可以直接构建到池中,或者它可以使用池进行动态内存调用,或者理论上它可以同时进行。我希望能够以调用我的池“alloc()”和“free()”函数的方式重载operatornew和operatordelete,以便获取构建对象所需的内存。我遇到的主要问题之一是让我的运算符(operator)删除以便能够通过调用我编写的pool->free()函数来释放内存。我想出了一个hack,通过将池传递到构造函数并让析构函数执行释放工作来修复它。这一切都很好而且花花公子,直到有人需要从这

c++ - 为什么我不能在 'std::deque' 上使用 operator< ?

在我的代码库上运行cppcheck并收到以下错误:Dangerousiteratorcomparisonusingoperator但是双端队列的迭代器是随机访问迭代器,随机访问迭代器支持不等式运算符。那么是什么给了?例子:#includeintmain(){std::dequed;std::deque::iteratordi1=d.begin();std::deque::iteratordi2=d.end();if(di1编辑:此错误已通过cppcheckticket#5926提交并修复. 最佳答案 这是cppcheck中的一个错误

c++ - 从 std::deque 线程安全地同时调用 emplace_back() 和 operator[]() 吗?

来自emplace_back()的文档摘录:IteratorvalidityAlliteratorsrelatedtothiscontainerareinvalidated,butpointersandreferencesremainvalid,referringtothesameelementstheywerereferringtobeforethecall.DataracesThecontainerismodified.Nocontainedelementsareaccessedbythecall:concurrentlyaccessingormodifyingthemissafe

c++ - 一个 const std::function 包装一个非常量 operator()/mutable lambda

考虑以下示例:#include#includestructA{inti;voidoperator()(){std::coutconst&fun){fun();}intmain(){conststd::functionf{A{}};test(f);test(f);}在这里,conststd::function能够调用非constoperator().输出:12如果我提供一个mutablelambda,也会发生同样的情况,例如test([x=0]()mutable{++x;});这怎么可能?conststd::function可以包装可变仿函数是否正常? 最佳答

c++ - 在类定义之外为模板类定义 operator[]()(数组订阅)

我认为这很容易,但它并没有按我预期的方式工作。这里的正确语法是什么?TemplateClass.htemplateclassTemplateClass{T&operator[](size_tn);TemplateClass.cpp#include"TemplateClass.h"templateT&TemplateClass::operator[](size_tn){//memberdeclarationnotfound} 最佳答案 您需要提供整个类名——包括模板参数:templateT&TemplateClass::operato

c++ - 错误 : copy assignment operator not allowed in union

当出现以下错误时,我正在编译下面的代码。我找不到原因。typedefunion{struct{constintj;}tag;}X;intmain(){return0;}error:member`::``::tagwithcopyassignmentoperatornotallowedinunion虽然这段代码使用gcc编译罚款。仅使用g++时出错。 最佳答案 为了拥有某个类类型T的union成员,T的特殊成员函数(默认构造函数、复制构造函数、复制赋值运算符、和析构函数)必须是微不足道的。也就是说,它们必须是由编译器隐式声明和定义的。

c++ - 将 raw operator new、placement new 和 standard delete 结合起来是否合法?

伙计们!出于好奇——以下代码可能不合法,对吗?T*p=::operatornew(sizeof(T));//allocatememoryforaTnew(p)T;//constructaTintotheallocatedmemorydeletep;//deletetheobjectusingthestandarddeleteoperator 最佳答案 没有。您只能删除从新返回的内容-没有异常(exception)。 关于c++-将rawoperatornew、placementnew和s

c++ - 防止继承 operator new 和 delete

(我确实扫描了,但找不到任何类似的东西,如果有欺骗请关闭)。有没有办法防止这两个运算符被继承?例如:structfoo{staticvoid*operatornew(std::size_t){//special}staticvoidoperatordelete(void*p,std::size_t){//special}};structbar:publicfoo{};现在bar将继承这两个运算符-在这种微不足道的情况下,没什么大不了的,如果foo和bar中有数据成员,就会出现问题(在我的情况下更糟,因为foo的分配需要与bar不同!)现在避免这种情况的方法是在bar,我也会实现操作符。

C++ : friend function in a template class for operator<<

在.cpp文件中声明模板类的友元函数(对于std::ostream&运算符?我当前的实现不起作用://MyTest.htemplateclassMyTest{inlinefriendstd::ostream&operator(std::ostream&lhs,constMyTest&rhs);};//MyTest.cpptemplateinlinefriendstd::ostream&operator(std::ostream&lhs,constMyTest&rhs){//IMPLEMENTATION}非常感谢! 最佳答案 引用op

c++ - operator()() 定义了什么?

如果这个问题被举报,我很抱歉,但我似乎无法轻松地在线找到解决方案。如果我覆盖operator()()这定义了什么行为? 最佳答案 operator()是函数调用操作符,即可以使用对应类型的对象作为函数对象。第二组括号包含空的参数列表(和往常一样)。例如:structfoo{intoperator()(){return17;};};intmain(){foof;returnf();//useobjectlikeafunction}上面的例子只是展示了运算符是如何声明和调用的。实际使用可能会访问运算符中的成员变量。函数对象在标准C++库