jjzjj

operator

全部标签

c++ - 全局重载运算符 new/new[] delete/delete[] C++

我有这个重载operatornew和delete的示例代码#include#include#include#ifdefUSE_ZMALLOCextern"C"{#include"zmalloc.h"}#definem_malloczmalloc#definem_freezfree#else#ifdefUSE_JEMALLOC#include#definem_mallocje_malloc#definem_freeje_free#else#include"malloc.h"#definem_mallocstd::malloc#definem_freestd::free#endif#end

c++ - 显式转换运算符模板的优先级和歧义

我一直在我的项目中使用模板化的显式转换运算符,以实现从自定义类变体类型的显式转换。重现我的问题的最小示例如下所示(在C++14模式下):#include#include#includeusingnamespacestd;classA{public:templateexplicitoperatorT()const//1{coutexplicitoperatorconstT&()const//2{coutexplicitoperatorT&()//3{cout(a)-3.14)我遇到的问题是为static_cast转换选择的运算符。对于GCC,这是一种预期的(1)情况。输出是:operat

python - operator[] 的 Swig 警告抑制

我想在python中访问一些C++。动态数组.h:#include#includenamespacemspace{templateclassDynamicArray{//letspiggybackonstd::vector.std::vectorm_implementation;public:typedefstd::vector::size_typesize_type;typedefT&reference;typedefconstT&const_reference;const_referenceoperator[](size_typei)constnoexcept(false){if(

c++ - 用户调用 operator new 时的分配/对象详细信息拦截和收集问题

我正在开发一个小型内存工具,它可以跟踪分配和释放、对象大小、对象类型等。我用来跟踪源文件、行号和对象类型的方法是这样工作的:#defineDEBUG_NEWSourcePacket(__FILE__,__LINE__)*new#definenewDEBUG_NEWSourcePacket只是一个小类,它在构造期间接受一个constchar*和一个int。这些值通过__FILE__和__LINE__宏填充。对象类型是这样获取的:templateT*operator*(constSourcePacket&packet,T*p);p是指向新分配对象的指针,其类型使用RTTI发现。在运算符重载

c++ - 模板类容器自动转换问题

我有以下代码:#includetemplateclassfixed{public:explicitfixed(doubleval=0){operator=(val);}~fixed(){}operatordouble()const{returnv_;}doubleoperator=(constdouble&d){if(d>Wt)v_=Wt;elsev_=d;returnv_;}private:doublev_;};intmain(){fixedx;std::vector>v(5);//std::vector>v(5,0);//fixedy;//v[0]=0;x=x*v[0];}在VS2

c++ - 在多个线程中重写方法

我已经实现了我自己的内存管理器,我像这样重写了new和delete运算符:/**OverridetheStandardC++newoperator*/void*operatornew(size_tsize);/**OverridetheStandardC++deleteoperator*/voidoperatordelete(void*p);这工作正常,但现在我正在一个具有大量堆分配的多线程环境中进行开发。为了避免堆争用,我希望每个线程都有自己的内存管理器实例。如何使运算符覆盖指向该线程的内存管理器实例? 最佳答案 线程本地存储可以

c++ - Operator = 不是 DLL 中导出的 C++ __interface 的成员

我一直在为库编写一些代码,并尝试使用默认的Win32控制台应用程序来运行所有内容。因为我已经完成了所有类(class),所以我想将所有内容提取到一个DLL中,因此我开始使用通常的宏进行改编:#ifdefMYLIB_EXPORTS#defineDllExport__declspec(dllexport)#else#defineDllExport__declspec(dllimport)#endif我在我的代码中使用一个接口(interface),它是这样定义的:__interfaceDllExportISerializable{voidSerialize(/*...*/);/*someo

c++如何为类的模板化转换运算符指定参数

我正在尝试为类的模板化转换运算符指定模板参数,但我似乎无法获得正确的语法。#includeusingnamespacestd;classC{inti_;public:C(inti):i_(i){}templateintget(){returni_+adder;}templateintoperator()(){returni_+adder;}templateoperatorint(){returni_+adder;}//IfIaddadefaultargumenttooperatorint()'saddertemplateparameterthiscompilesfine//(ofcou

c++ - 为什么某些 C++ 标准库 `operator<<` 函数将其流宽度重置为 0?

如C++标准的[ostream.inserters.character]部分所述,在将char或字符串插入流后,流的宽度设置为0:templatebasic_ostream&operator&out,constunsignedchar*s);Dolotsofsensibleandexpectedthings...Callswidth(0).为什么C++标准要求调用width(0)?更改流宽度而不将其重置为原始值的合理性是什么?(据我所知,流的所有其他属性都由流插入运算符保留。)有个相关问题here,这说明了这引起的困惑,但没有解释为什么标准规定了这种行为。

c++ - 带有默认参数的模板类的 operator<< 中的 lambda 错误

谁能告诉我这段代码出了什么问题:template//data;templatevoiditerate(Ff)const{for(unsignedi=0;i&a)//Line17{a.iterate([&os](unsignedi,constB&x){osGCC4.8.1和--std=c++11的错误消息:test.cpp:Infunction‘std::ostream&operator&)’:test.cpp:17:41:error:defaultargumentfortemplateparameterforclassenclosing‘operator&)::__lambda0’a.