草庐IT

c++ - MyClass 对象 = MyClass(); 'MyClass()' 在这里指的是一个临时对象吗?

考虑不涉及复制省略的情况(C++17之前)。来自cppreference(再次假设C++14):Temporaryobjectsarecreatedinthefollowingsituations:bindingareferencetoaprvaluereturningaprvaluefromafunctionconversionthatcreatesaprvaluelambdaexpressioncopy-initializationthatrequiresconversionoftheinitializerlist-initializationthatconstructsanstd

c++ - 为什么我必须在这里清除 std::stringstream?

我写了一个简短的测试程序,看看我是否可以重复使用stringstream附加到一个字符串。在第一个版本中,我得到了Output1,但我真的不明白为什么s1保持为空。我发现我必须执行ss.clear()然后在Output2中得到预期的结果。任何人都可以解释为什么没有清除它就不起作用吗?我本以为,如果我重复输入数字并将它们取回一个字符串,我应该总是得到数字。我不确定是否附加了数字,但这不是本示例的重点。此处:http://www.cplusplus.com/reference/sstream/stringstream/它说我可以使用任何操作,并且没有限制或要求重置字符串,我可以看到。我也不

c++ - 为什么 enable_if 不能在这里工作?

我有这段代码,我期望会有两个不同版本的运算符()基于模板参数的类型。#include#includetemplatestructImpl{std::enable_if_t::value,T>operator()(conststd::string&key,intnode){returnstatic_cast();}std::enable_if_t::value,T>operator()(conststd::string&key,intnode){returnnewT();}};intmain(){}相反,我在编译时遇到错误:'std::enable_if_t::value,T>Impl:

c++ - 这里调用了哪个构造函数?

在这段代码中,实际调用了哪个构造函数?Vectorv=getVector();Vector有复制构造函数、默认构造函数和赋值运算符:classVector{public:...Vector();Vector(constVector&other);Vector&operator=(constVector&other);};getVector按值返回。VectorgetVector();代码使用C++03标准。代码片段看起来应该调用默认构造函数然后调用赋值运算符,但我怀疑这个声明是使用复制构造函数的另一种形式。哪个是正确的? 最佳答案

c++ - 为什么 __builtin_prefetch 在这里没有任何作用?

我正在编写一个程序来解析文件。它由一个逐个字符解析并处理它们的主循环组成。这是主循环:charc;char*ptr;for(size_ti=0;i(sentenceMap)+i);c=*ptr;__builtin_prefetch(ptr+i+1);//sometreatmentonptrandc}如您所见,我添加了一条builtin_prefetch指令,希望在我的循环的下一次迭代中放入缓存。我尝试了不同的值:ptr+i+1、ptr+i+2、ptr+i+10,但似乎没有任何变化。为了衡量性能,我使用了valgrind的工具cachegrind,它可以指示缓存未命中数。在c=*ptr行

c++ - 重载函数。怎么这里模棱两可?

我在下面的代码中重载了我的函数:voidfunction(charx,doubley){cout当我尝试编译时它告诉我:“[警告]ISOC++说这些是模棱两可的,即使第一个最差的转换比第二个最差的转换要好”编译器如何在此处进行隐式转换,以致于不清楚哪个候选者是正确的? 最佳答案 文字常量'a'和'b'的类型为char,因此没有完全匹配。出现歧义是因为第一个参数与第一个函数匹配,但第二个参数的首选转换是int,与第二个函数匹配。GCC对此非常明确,发出以下诊断:warning:ISOC++saysthattheseareambiguo

c++ - 我可以在这里使用 Curiously Recurring Template Pattern (C++) 吗?

我有一个C++应用程序,可以简化为如下所示:classAbstractWidget{public:virtual~AbstractWidget(){}virtualvoidfoo(){}virtualvoidbar(){}//(othervirtualmethods)};classWidgetCollection{private:vectorwidgets;public:voidaddWidget(AbstractWidget*widget){widgets.push_back(widget);}voidfooAll(){for(unsignedinti=0;ifoo();}}void

c++ - 松弛的内存顺序会导致这里无限循环吗?

有问题的代码:#include#includestd::atomic_boolstop(false);voidwait_on_stop(){while(!stop.load(std::memory_order_relaxed));}intmain(){std::threadt(wait_on_stop);stop.store(true,std::memory_order_relaxed);t.join();}由于此处使用了std::memory_order_relaxed,我假设编译器可以在t.join()stop.store()。结果,t.join()永远不会返回。这个推理正确吗?如

c++ - 这里的 (void) first2++ 有什么意义?

这个问题在这里已经有了答案:Whydoesstd::transformandsimilarcastthe'for'loopincrementto(void)?(2个答案)关闭5年前。关于thisen.cppreference的页面有可能实现词典顺序比较的示例。这是基本的:templateboollexicographical_compare(InputIt1first1,InputIt1last1,InputIt2first2,InputIt2last2){for(;(first1!=last1)&&(first2!=last2);first1++,(void)first2++){if

c++ - 为什么打包结构的大小在这里是 5 而不是 4 字节?

参见在线示例:Ideoneexamplestruct{union{struct{uint32_tmessageID:26;uint8_tpriority:3;}__attribute__((packed));uint32_trawID:29;}__attribute__((packed));uint8_tcanFlags:3;}__attribute__((packed))idSpecial;为什么编译器会将结构的大小报告为5个字节而不是此处的4个字节?它应该包含32位。 最佳答案 问题是__attribute__((packed)