草庐IT

copy_ctor_assign

全部标签

c++ - 在 C 中,将函数指针赋值给适当类型的变量给出 "cannot convert ... in assignment"

采用以下C/C++代码:#includeintinc(inti){returni+1;}//int→int,likeabs()//bazisbool→(int→int)int(*baz(boolb))(int){returnb?&abs:&inc;}intmain(){int(*foo(bool))(int);//foois&(bool→(int→int))foo=baz;}尝试编译这个(gcc或g++)给出:$g++test.cctest.cc:Infunction‘intmain()’:test.cc:9:error:assignmentoffunction‘int(*foo(bo

c++ - 为什么调用了错误的 ctor?

我有按预期工作的代码:EscapedStringes("Abc&def");EscapedStringes2("");es2=es;//es2==Abc%26def以及未按预期工作的代码:EscapedStringes("Abc&def");EscapedStringes2=es;//es==Abc%2526def在第二种情况下,即使es是EscapedString,也会调用CTOR2而不是CTOR3。EscapedStringes(EscapedString("Abc?def"));做正确的事,但我似乎无法在CTOR3上设置断点,所以我不确定它是否正常工作,或者代码已被优化掉或意外工

c++ - 使用 std::copy 复制所有元素的地址

我正在尝试获取给定集合的所有元素的地址并将它们复制到std::set。基本上,而不是std::sets1;std::copy(first1,last1,std::inserter(s1,s1.begin()));我想插入他们的地址。像这样的东西:std::set>s1;std::copy(reference_iterator(first1),reference_iterator(last1),std::inserter(s1,s1.begin()));在这里,reference_iterator将是一个迭代器,返回其元素的地址,而不是元素,这与boostindirect_iterato

c++ - 在 copy-and-swap 习语中实现交换

正在关注Whatisthecopyandswapidiom和Howtoprovideaswapfunctionformyclass,我尝试像后者接受的答案选项2那样实现交换函数(具有调用成员函数的自由函数),而不是前一个链接中的直接友好自由函数。但是下面的不编译#include//Uncommentingthefollowingtwolineswon'tchangethestateofaffairs//classBar;//voidswap(Bar&,Bar&);classBar{public:Bar(unsignedintbottles=0):bottles(bottles){enf

C++ 标准 : return by copy to initialize a reference without RVO: is there any copy?

让我们考虑下一个示例:structbig_type{};//Returnbycopyautofactory(){returnbig_type{};}voidany_scope_or_function(){big_type&&lifetime_extended=factory();}假设RVO被禁止或根本不以任何方式存在,big_type()是否会或可以被复制?还是将引用直接绑定(bind)到return语句中构造的临时对象?我想确保big_type析构函数仅在any_scope_or_function结束时被调用一次。我使用C++14,以防某些行为在标准版本之间发生变化。

【Golang map并发报错】panic: assignment to entry in nil map

go并发写map[string]interface{}数据的时候,报错:panic:assignmenttoentryinnilmap多个key同时操作一个map时,如:test[key1]=1test[key2]="a"test[key3]=true就会遇到并发nil值报错,什么test[key-xxx]=make()根本不行。用异步sync.Map解决://map[string]interface{}全局配置(自定义)参数。读写varsyncMapInterfacesync.Map//SetGlobalMapInterface新增或更新funcSetGlobalMapInterface(k

c++ - 为什么这里调用的是 Copy Constructor 而不是普通的 Constructor 和重载的赋值运算符?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:IsthereadifferenceinC++betweencopyinitializationanddirectinitialization?CopyconstructorsandAssignmentOperators我有一个C类,我在其中重载了Normal、复制构造函数和赋值运算符以打印被调用内容的踪迹。我写了以下代码来测试什么时候被调用?Cc1;-->NormalConstuctor..//understoodFineCc2;c2=c1;-->Normalconstructor+assignmentop

c++ - copy-and-swap 技术在赋值运算符函数中使用复制构造函数

我正在阅读“EffectiveC++byScottMeyers”,其中第11项建议在我的赋值运算符中使用“copy-and-swap”技术:Widget&Widget::operator=(constWidget&rhs){Widgettemp(rhs);//Copyconstructorswap(temp);//Swapwith*thisreturn*this;}但是在Item12中是这样写的:Itmakesnosensetohavecopyassignmentoperatorcallthecopyconstructor.我认为第11项和第12项是矛盾的。我理解错了吗?

c# - 无法在我的 WPF 应用程序中使用 Array.Copy()

我是一名C++开发人员,最近开始从事WPF方面的工作。好吧,我在我的应用程序中使用Array.Copy(),看起来我无法完全获得所需的结果。我在我的C++应用程序中做了如下操作:staticconstsignedcharversion[40]={'A','U','D','I','E','N','C','E',//name0,0,0,0,0,0,0,0,//reserved,firmwaresize0,0,0,0,0,0,0,0,//boardnumber0,0,0,0,0,0,0,0,//variant,version,serial0,0,0,0,0,0,0,0//datecode,r

c++ - 对于具有抛出复制构造函数和 noexcept 按值复制赋值的类,is_nothrow_copy_assignable 的值是多少?

根据C++标准,以下程序的预期(如果有)输出是什么:#include#include#includeclassA{public:A()=default;~A()=default;A(Aconst&other){}A(A&&other)noexcept{}A&operator=(Aother)noexcept{return*this;}};intmain(){std::cout::value::value换句话说,类型特征值的评估是否只看赋值运算符的声明,即noexcept,并因此产生truetrue或者它是否考虑调用上下文(a、b是A的实例)a=b;//maythrow,implici