草庐IT

hljs-operator

全部标签

c++ - 为什么 std::chrono::duration::operator*= 不像内置的 *=?

如std::chrono::duration::operator+=中所述签名是duration&operator*=(constrep&rhs);这让我很奇怪。我假设持续时间文字可以像任何其他内置一样使用,但事实并非如此。#include#includeintmain(){usingnamespacestd::chrono_literals;autom=10min;m*=1.5f;std::cout输出是150%of10min:10min150%of10:15为什么这样选择界面?在我看来,这样的界面templateduration&operator*=(constT&rhs);会产生

c++ - 使用继承时实现 operator==

我有一个实现==运算符的基类。我想写另一个类,继承基类,并且应该重新实现==运算符。这是一些示例代码:#include#includeclassPerson{public:Person(std::stringName){m_Name=Name;};booloperator==(constPerson&rPerson){returnm_Name==rPerson.m_Name;}private:std::stringm_Name;};classEmployee:publicPerson{public:Employee(std::stringName,intId):Person(Name)

c++ - 辛>> "no operator matches these operands"

我一直在visualstudio2012控制台模式下处理一个C++项目,我一直在使用cin函数时遇到这个奇怪的持续性错误。在>>>下,我得到一条红线,程序告诉我没有运算符匹配这些操作数。我已经在单独的方法中初始化了所有数组元素。这是一个片段示例(实际代码包含更多变量):for(inti=0;i>allTaxiDetails[i].taxiRank;}allTaxiDetails是一个数组,数据类型为“taxiDetails”,结构如下:structtaxiDetails{stringtaxiDriverSurname;inttaxiID;inttaxiCoordinates;intnu

c++ - libstdc++ 中的 ostream operator<< 是线程敌对的吗?

ostream运算符(operator)使用num_put::put()用于数字格式化。我正在尝试遵循代码。我将链接到OSX文件,但类似的文件出现在我看过的其他一些系统上。在我看来num_put::put()电话num_put::do_put(),它调用num_put::_M_insert_float(),这calls__convert_from_v():http://www.opensource.apple.com/source/libstdcxx/libstdcxx-60/include/c++/4.2.1/bits/c++locale.hhttp://www.opensource

c++ - 绑定(bind) lambda 的速度(通过 std::function)与仿函数结构的 operator()

autolam=[](inta,intb,intc){returna在版本一中,我们std::vector>lamvals;//getparametersandforeachlamvals.emplace_back(std::bind(lam,a,std::placeholders::_1,b));替代方案是std::vectorlamvals;//getparametersandforeachlamvals.emplace_back(functor{a,b});在这两种情况下我们都有一个简单的迭代returnstd::any_of(lamvals.cbegin(),lamvals.c

c++ - boost shared_ptr : difference between operator= and reset?

下面两段代码有区别吗?它们中的任何一个比另一个更可取吗?运算符=boost::shared_ptrfoo;//foo.ptrshouldbeNULLfoo=boost::shared_ptr(newBlah());//Involvescreationandcopyofashared_ptr?重置boost::shared_ptrfoo;//foo.ptrshouldbeNULLfoo.reset(newBlah());//foo.ptrshouldpointnowtoanewBlahobject注意:我需要定义shared_ptr然后将其设置在不同的行中,因为我在一段代码中使用它,例如

c++ - 这是实现通用 operator== 和 operator< 的安全方法吗?

看到thisquestion之后,我的第一个想法是定义通用等价和关系运算符是微不足道的:#includetemplatebooloperator==(constT&a,constT&b){returnstd::memcmp(&a,&b,sizeof(T))==0;}templatebooloperatorusingnamespacestd::rel_ops然后会变得更有用,因为它会被运算符的默认实现完全通用==和.显然,这不会执行成员比较,而是按位比较,就好像该类型只包含POD成员一样。这与C++生成复制构造函数的方式并不完全一致,例如,确实执行成员复制。但是我想知道上面的实现是否真的

c++ - 使用 bool operator== 比较对象

所以,看了一些SO问答,还是不明白为什么要用friendbooloperator==(BaseClassconst&left,BaseClassconst&right)代替booloperator==(BaseClassconst&right)现在我有这样的东西http://pastebin.com/pKsTabC0(已修复)-它似乎工作正常。但也许我错过了什么?有什么建议吗?更新1好的,我更改了源以使其正常工作http://ideone.com/fIAmB.删除了不必要的virtual并添加了const。我仍然不明白为什么要使用friend... 最佳答案

c++ - 将默认构造的迭代器与 operator== 进行比较

C++标准是否说我应该能够比较两个默认构造的STL迭代器是否相等?默认构造的迭代器是否具有相等可比性?我想要以下内容,例如使用std::list:voidfoo(conststd::list::iteratoriter){if(iter==std::list::iterator()){//Something}}std::list::iteratori;foo(i);我在这里想要的是类似于迭代器的NULL值,但我不确定它是否合法。在VisualStudio2008附带的STL实现中,它们在std::list的operator==()中包含断言以排除这种用法。(他们检查每个迭代器是否由同一

c++ - 当我只想禁用它时,自定义赋值 operator=() 的签名是否重要?

我需要禁用复制赋值运算符。这将起作用:A&operator=(constA&);如果我不为operator=指定确切的参数,它会工作吗?我的意思是这样的:voidoperator=(void);返回值是对的,我可以随便写,但是参数类型呢?这会覆盖类的默认operator=吗? 最佳答案 来自12.8p17C++标准草案:Auser-declared copy assignmentoperatorX::operator= isanon-staticnon-templatememberfunctionofclass X withexac