草庐IT

conditional-operator

全部标签

c++ - 不能将 operator= 与 std::stringstream 一起使用

我正在尝试制作struct,其中一名成员属于std::stringstream类型。我正在使用C++11,并根据http://www.cplusplus.com/reference/sstream/stringstream/operator=/我能行。这是我的代码:structlogline_t{stringstreamlogString;/*!它没有编译,因为我收到这个错误:error:useofdeletedfunction‘std::basic_stringstream&std::basic_stringstream::operator=(conststd::basic_stri

c++ -::operator new(size_t) 是否使用 malloc()?

::operatornew(size_t)是在内部调用malloc(),还是直接使用系统调用/操作系统特定的库调用?C++标准怎么说?在thisanswer它说:malloc()isguaranteedtoreturnanaddressalignedforanystandardtype.::operatornew(n)isonlyguaranteedtoreturnanaddressalignedforanystandardtypenolargerthann,andifTisn'tacharactertypethennewT[n]isonlyrequiredtoreturnanaddr

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++ - cout 语句中使用的条件运算符

通过尝试,我开始知道有必要在cout语句中将条件运算符放在括号中。这里有一个小例子:#includeintmain(){inta=5;floatb=(a!=0)?42.0f:-42.0f;//worksfinestd::cout输出是:42421为什么需要这些括号?在这两种情况下,条件运算符的结果类型都是已知的,不是吗? 最佳答案 ?:运算符的优先级低于运算符,即编译器将您的最后一条语句解释为:(std::cout这将首先流式传输(a!=0)的bool值计算。然后该表达式的结果(即对cout的引用)将被转换为适当的类型以用于?:运算

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++ - <mutex> 和 <condition_variable> 的异常处理

假设没有发生未定义的行为,没有发生死锁,互斥锁被正确的线程以正确的顺序锁定和解锁正确的次数,非递归互斥锁不会被多次锁定,锁定递归互斥量不超过maximumlevelofownership,没有谓词传递给条件变量,并且只有标准库提供的时钟、时间点和持续时间与std::互斥锁和条件变量一起使用是否保证对不同类型的std::互斥量和条件变量进行操作(除了构造它们)不会抛出任何异常(尤其是类型std::system_error)?例如,在以下方法的情况下:voidMyClass::setVariable(){std::lock_guardconstguard(m_mutex);m_var=42

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然后将其设置在不同的行中,因为我在一段代码中使用它,例如