草庐IT

operation_type

全部标签

c++ - 为什么定义 operator+ 来调用 operator+= 比相反更有效?

这是C++Primer5thEdition中的练习:Exercise14.14:Whydoyouthinkitismoreefficienttodefineoperator+tocalloperator+=ratherthantheotherwayaround?(P.561)鉴于operator+=和operator+的实现:Sales_data&Sales_data::operator+=(constSales_data&rhs){units_sold+=rhs.units_sold;revenue+=rhs.revenue;return*this;}Sales_dataoperat

C++ 错误 : Expected a type specifier

当我尝试像这样使用LoggerStream时,我得到“需要一个类型说明符”:constLoggerStreamlogger(L"测试组件");这是我尝试使用LoggerStream的地方:#include"Logger.h"#include"TestComponent.h"namespaceophRuntime{structTestComponent::TestComponentImpl{private:LoggerStreamlogger(L"TestComponent");NO_COPY_OR_ASSIGN(TestComponentImpl);};TestComponent::T

c++ - 奇怪的异常抛出 - 分配 : Operation not permitted

我想从cin进行异步读取,因此我有一段代码客户端.h...boost::asio::posix::stream_descriptorinput;boost::asio::streambufinput_buffer客户端.cppClient::Client(intargc,char**argv,boost::asio::io_service&io_service):tcp_socket(io_service),udp_socket(io_service),input(io_service,::dup(STDIN_FILENO)){...read_std_input();}voidClie

c++ - 用户定义类型的 std::common_type 特征

自C++11以来,引入了类型特征std::common_type。std::common_type确定其所有模板参数之间的公共(public)类型。在C++14中,还引入了辅助类型std::common_type_t以缩短使用std::common_type类型特征的代码。std::common_type在重载算术运算符中特别有用,例如,templatestd::common_type_toperator+(T1const&t1,T2const&t2){returnt1+t2;}如果它的模板参数是内置类型(例如,int、double),它会工作得很好。但是,如果我将用户定义的类型作为模

c++ - 奇怪的运算符重载, "operator T& () const noexcept { return *_ptr; }"

我研究了一下,operator函数的格式是(returnvalue)operator[space]op(arguments){implementation}但是,在std::reference_wrapper实现中,有一个运算符重载函数声明为operatorT&()constnoexcept{return*_ptr;。这个运算符和T&operator()constnoexcept{return*_ptr;不同吗?}?.如果两者不同,那么第一个有什么用? 最佳答案 运算符T&()constnoexcept;是一个user-define

c++ - 重载 "operator++"返回一个非常量,clang-tidy 提示

我刚从clang-tidy收到以下警告:overloaded"operator++"returnsanon-constantobjectinsteadofaconstantobjecttypehttps://clang.llvm.org/extra/clang-tidy/checks/cert-dcl21-cpp.html不幸的是,他们提供的链接不起作用,https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=88046682没有简单的方法可以准确地找到这个规则(貌似DCL规则是从50开始的)。但是无论我在标准的

c++ - 如果我想让类不可复制, "operator="返回类型是否重要?

假设我有一个不支持成员复制的类,所以我不想保留编译器实现的复制构造函数和赋值运算符。我也不想实现这些,因为要么这样做需要额外的努力,我不需要在我的类里面进行这些操作,或者那些操作在我的类里面没有意义所以我想禁止他们。这样做I'lldeclarethemprivateandprovidenoimplementation:classNonCopyable{private:NonCopyable(constNonCopyable&);//notimplementedanywherevoidoperator=(constNonCopyable&);//notimplementedanywher

c++ - 无法将参数 1 从 'cli::interior_ptr<Type>' 转换为 'CvCapture **'

我正在抓取一个视频帧如下CvCapture*capture=cvCreateFileCapture("PATH");我可以阅读视频并处理它。一切正常。但是当我尝试释放捕获时cvReleaseCapture(&capture);我明白了errorC2664:'cvReleaseCapture':cannotconvertparameter1from'cli::interior_ptr'to'CvCapture**'with[Type=CvCapture*]Cannotconvertamanagedtypetoanunmanagedtype函数在一个类中。publicrefclassLoc

c++ - 使用 std::string 时为 "error: no match for ‘operator<<"

你能帮我找出下面代码中的问题吗(代码类似于C++streamasaparameterwhenoverloadingoperator):#include#includeclasslogger{public:voidinit(std::ostream&ostr){stream=&ostr;}templatelogger&operator一切正常,直到我取消注释包含“world”的行。在这种情况下,GCC产生错误:在...中与“operator有意思的是VS2008对这段代码没有问题谢谢! 最佳答案 std::string("world"

c++ - 我可以在 vector 的一个实例上使用 value_type,而不是它的类型吗

在播放和尝试计算vector的总大小时,我尝试了类似的方法vectorvd;autoarea=vd.size()*sizeof(vd::value_type);//IveseenStepanovuseareaasnameforthiskindofsize,idkifheaddsthesizeofvdalsotoarea:)不幸的是,这不起作用......我需要使用vector::value_type但这会降低代码的可读性。它可以工作吗?我不喜欢sizeofvd.front()因为写front()看起来很难看为此。编辑:decltype变体也适合我所说的丑陋类别......