草庐IT

binary-arithmetic-operations

全部标签

c++ - 为什么要避免 C++ 中的输入运算符(operator>>)?

在thisquestion接受答案的作者不推荐使用输入运算符,因为:operator>>issubjecttoiomanipstreammanipulatorsandother"funny"stuff,soyoucanneverbesurethatitdoeswhat'sadvertised.但这意味着什么?为什么我们在用C++编程时要避免使用C++的输入运算符?我从TheDefinitiveC++BookGuideandList读过的书没有提到这一点,他们引入并使用了输入运算符。我也没有在互联网上找到任何关于这个主题的有用信息。谢谢!p.s:很抱歉,我没有足够的声望在该主题中提出我的

c++ - 在 `using Base::operator T` 是模板类型参数的情况下,是否允许 `T`?

考虑这个例子:structB{operatorint();};templatestructX:B{usingB::operatorT;};GCC接受代码,而ClangMSVC拒绝它。哪个是正确的?注意,如果基类型是依赖的,所有的编译器都接受代码:templatestructB{operatorT();};templatestructX:B{usingB::operatorT;}; 最佳答案 我认为GCC是对的,在§7.3.3/1中,我们可以找到:Thesetofdeclarationsintroducedbytheusing-dec

c++ - msvs12 将大括号视为 operator()

#includestructA{voidoperator()(constchar*){std::coutmsvs12对这段代码很满意,但我不明白为什么。是bug还是别的什么?更新:我尝试使用msvs2013(v12.0.31101.0更新4) 最佳答案 是的,这是一个错误。Hereistheticketforit仍然打开。 关于c++-msvs12将大括号视为operator(),我们在StackOverflow上找到一个类似的问题: https://stac

c++ - packaged_task 卡在 operator() 上

在Ubuntu上使用gcc4.7.2编译,使用-std=c++11-O0-pthread编译,我以某种方式在代码中创建了一个死锁,它似乎不应该遇到这个问题。我有一个线程,它刚刚获得一个锁,然后运行​​vector>,调用一切。同时,主线程推送std::packaged_tasks一个接一个地处理它,并在该任务的future时阻止返回。任务本身是微不足道的(打印和返回)。这是完整的代码。运行应用程序有时会成功,但尝试几次就会挂起:#include#include#include#include#includestd::unique_locklock(){staticstd::mutexm

c++ - 为什么 std::array 临时 constexpr 的 operator[] 不是?

我正在将一些值填充到constexprstd::array中,然后当我发现你不能时将编译时静态优点继续到更多constexpr值中在C++11中使用元素作为constexpr初始化器。这是因为std::array::operator[]实际上直到C++14才被标记为constexpr:https://stackoverflow.com/a/26741152/688724编译器标志升级后,我现在可以使用constexprstd::array的元素作为constexpr值:#includeconstexprstd::arrayarray{{3}};//Initializeaconstexp

c++ - 为什么 std::binary_search 返回 bool?

根据草案N4431,函数std::binary_search在算法库中返回bool,[binary.search]:templateboolbinary_search(ForwardIteratorfirst,ForwardIteratorlast,constT&value);templateboolbinary_search(ForwardIteratorfirst,ForwardIteratorlast,constT&value,Comparecomp);Requires:Theelementseof[first,last)arepartitionedwithrespecttoth

c++ - 为什么我不能像 someClassObject++5 那样在后缀 operator++ 中使用虚拟参数?

我知道我可以在后缀operator++中使用虚拟int作为someClassObject.operator++(5),但为什么不能像someClassObject++5那样使用它?我问这个是因为operator+可以像someClassObject1+someClassObject2一样使用。{public:test(intx,stringy):x(x),y(y){}test():x(0),y(""){};//skipingsomecodeforcopyconstructorandassignmentoperatoroverloadtestoperator++(intx){testa(

c++ - 在哪里可以找到 GNU stdc++ operator new 的源代码?

我在这里试过:http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/index.html我找到了new.h,但没有找到实现它的代码。谢谢 最佳答案 libstdc++相关源码可在线浏览here.您可能想阅读this. 关于c++-在哪里可以找到GNUstdc++operatornew的源代码?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/qu

c++ - 在从放置 new 获得的指针上使用 operator delete 的合法性

我很确定这段代码应该是非法的,因为它显然不起作用,但它似乎是C++0xFCD允许的。classX{/*...*/};void*raw=malloc(sizeof(X));X*p=new(raw)X();//accordingtothestandard,theRHSisaplacement-newexpression::operatordelete(p);//definitelywrong,perlitb'sanswerdeletep;//legal?Ihopenot也许你们中的一位语言律师可以解释标准是如何禁止这样做的。还有数组形式:classX{/*...*/};void*raw=m

c++ - 为什么 unique_ptr operator-> 不是 const-overloaded?

std::unique_ptr::operator->有签名pointeroperator->()constnoexcept;所以operator->是const但返回一个可变指针。这允许如下代码:voidmyConstMemberFunction()const{myUniquePtrMember->nonConstFunction();}为什么标准允许这样做,以及防止上述使用的最佳方法是什么? 最佳答案 把它想象成一个普通的指针:int*consti;是const指向非const的指针int.您可以更改int,但不是指针。intc