草庐IT

call_me_virtual

全部标签

c++ - std::call_once 是免费的吗?

我想知道std::call_once锁是否空闲。There是使用互斥锁的call_once实现。但是我们为什么要使用互斥体呢?我尝试使用atomic_bool和CAS操作编写简单的实现。代码线程安全吗?#include#include#include#includeusingnamespacestd;usingmy_once_flag=atomic;voidmy_call_once(my_once_flag&flag,std::functionfoo){boolexpected=false;boolres=flag.compare_exchange_strong(expected,tr

c++ - 为什么必须将 "virtual"放入基类而不是仅在子类中使用 "override"?在 C++ 中

标题几乎说明了一切。为什么要在基类中定义以后可能重写的函数?如果您想从中派生出不同行为的版本,那么您必须修改基类,并且您还没有考虑过将基类中的特定函数标记为虚拟。据我所知,您通过使用override解决了java中的此类抽象定义,这对我来说似乎更自然(尽管到目前为止我只关注java)。c++中的这种扭曲是为了强制程序员提前思考,还是有技术原因导致它这样工作? 最佳答案 这样做是为了使成员函数成为虚函数的决定保留在基类中。决定将一个函数设为虚函数会对设计产生影响:如果你将一个函数设为虚函数,你必须考虑它执行不同操作的可能性。相反,您可

c++ - std::call_once 和内存重新排序

给定来自here的代码:classlazy_init{mutablestd::once_flagflag;mutablestd::unique_ptrdata;voiddo_init()const{data.reset(newexpensive_data);}public:expensive_dataconst&get_data()const{std::call_once(flag,&lazy_init::do_init,this);return*data;}};我在其他地方也看到了相同模式的一些变体。所以我的问题是:为什么这段代码被认为是保存的?以及为什么编译器不能在调用std::c

c++ - "virtual base class in the case of multilevel inheritance"有意义吗

考虑以下显示多级继承的示例代码:案例1:这里类derived1是通过虚拟继承从类base派生的,类derived2是从类派生的直接类derived1。classbase{};classderived1:virtualpublicbase{};classderived2:publicderived1{};Case2:与Case1相同,只是不涉及虚拟继承classbase{};classderived1:publicbase//novirtualinheritance{};classderived2:publicderived1{};假设我在这两种情况下都创建了derived2类的对象。C

c++ - std::runtime_error 子类的 "call to deleted constructor of"编译器错误

我从std::runtime_error派生了一个异常类,以便添加对异常流的支持。我收到一个奇怪的编译器错误输出,我不确定如何解决?clang++-std=c++11-stdlib=libc++-g-Wall-I../-I/usr/local/includeMain.cpp-cMain.cpp:43:19:error:calltodeletedconstructorof'EarthException'throwEarthException(__FILE__,__LINE__)^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~../EarthExce

c++ - 最烦人的解析 : why doesn't `g( ( f() ) );` call `f` 's default constructor and pass the result to `g` 's ctor that takes a `f` ?

这不是Mostvexingparse:whydoesn'tAa(());work?的拷贝,它基于Aa());形式的解析,其OP认为可以使用额外的集合默认构造一个A对象括号。相比之下,我的问题是关于2个类,f和g,其中f具有默认构造函数,而g的构造函数采用f。我想用一个临时的f参数调用g的构造函数,而不使用统一的初始化语法。g的构造函数中有一个std::cout语句,因此缺少输出表示函数声明而不是g对象实例化。我在注释中用3个数字注释了示例代码。#1和#2编译时#3被注释掉,反之亦然:#includestructf{};structg{g(f){std::cout#1:我认为#1声明了一

c++ - 错误 : no matching function for call to ‘to_string(std::basic_string<char>&)’

即使在模板中我可以有任何类型,函数to_string对基本字符串不起作用:例如:std::stringstr("mystring");my_class(str);用这个仿函数定义:templatevoidoperator()(valuetypevalue){...private_string_field=std::to_string(value);不起作用。这是错误:error:nomatchingfunctionforcallto‘to_string(std::basic_string&)’避免它的最佳方法是什么。事先,我要求避免仅仅因为一些常见的关键字就链接到不相关的问题。

c++ - "no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’“

我正在尝试使用std::async创建线程,但我不断收到错误“没有匹配函数调用‘async(std::launch,,std::string&)’”在行上ConnectFuture=std::async(std::launch::async,Connect_T,ip);这是产生这种行为的代码:#includeclasslibWrapper{public:voidConnect(std::stringip);voidConnect_T(std::stringip);private:std::futureConnectFuture;};voidlibWrapper::Connect(std

c++ - 错误 : no matching member function for call to 'push_back'

为什么我在最后两行收到错误?目标是在集合中找到对象,并修改其内容。usingnamespacestd;structmystruct{intid;vectory;mystruct(constintid):id(id){}booloperatorsx;mystructx(1);x.y.push_back(1);x.y.push_back(2);sx.insert(x);//set::iteratori=sx.find(1);constmystruct*x1=&(*i);constmystructx2=*x1;couty)y)y.push_back(4);}好像迭代器返回的是常量对象,不让我

c++ - 复制初始化: why move or copy constructor was not called even if copy-elision is turned off?

我的问题不同,因为我可能“知道”复制省略。我正在学习复制初始化。但是,以下代码让我感到困惑,因为我已经使用-fno-elide-contructors-O0选项关闭了复制省略。#includeusingnamespacestd;classtest{public:test(inta_,intb_):a{a_},b{b_}{}test(consttest&other){cout我首先使用命令构建:g++-std=c++11-fno-elide-constructors-O0main.cpp-omain得到如下结果:**showelideconstructors**moveconstruct