草庐IT

implicit-function-declaration

全部标签

C++ : struct vs function for ordering elements

我有一个struct有两个字段:structroad{intfrom,len;};出于某种原因,我需要能够订购我的road:按升序from在数组中按升序len在优先队列中我因此包括:#include#include#include#include我遇到过建议重载operator的网站,但由于两种可能的顺序感觉不对,它只会解决两者之一。通过弄乱教科书,我得到了这个工作:boolcmpFrom(constroad&a,constroad&b){return(a.from用于:std::sort(trips,trips+nbRoads,&cmpFrom);std::priority_queu

c++ - 避免过多的函数参数 : class-centered or function-centered approach?

您将如何修复以下传递过多参数的错误代码?voidhelper1(intp1,intp3,intp5,intp7,intp9,intp10){//...}voidhelper2(intp1,intp2,intp3,intp5,intp6,intp7,intp9,intp10){//...}voidfoo(intp1,intp2,intp3,intp4,intp5,intp6,intp7,intp8,intp9,intp10){helper1(p1,p3,p5,p7,p9,p10);helper2(p1,p2,p3,p5,p6,p7,p9,p10);}我看到两种不同的方法:方法一:将所有函

c++ - 删除复制构造函数或复制赋值运算符是否算作 "user declared"?

根据thispresentation,如果复制构造函数或复制赋值运算符是“用户声明的”,则不会生成隐式move操作。删除复制构造函数或复制赋值运算符是否算作“用户声明”?structNoCopy{NoCopy(NoCopy&)=delete;NoCopy&operator=(constNoCopy&)=delete;};是否会为NoCopy类生成隐式move操作?还是删除相关复制操作算作“用户声明”,从而抑制隐式move生成?如果可能的话,我更喜欢引用标准相关部分的答案。 最佳答案 根据您演示文稿的幻灯片14,已删除的复制构造函数是

c++ - 错误 C2280 : attempting to reference a deleted function (atomic<int>)

我有一个classA带有成员变量_atomicVar类型std::atomic.#includeclassA{public:A();~A();private:std::atomic_atomicVar;};如果我构建项目,我会收到以下错误:errorC2280:'std::atomic::atomic(conststd::atomic&)':attemptingtoreferenceadeletedfunction我主要是一名C#开发人员,所以我还不了解C++的每个细节(还)。我不知道我在哪里使用atomic的复制代码.我还尝试初始化_atomicVar:std::atomic_ato

c++ - 为什么 std::is_function 对简单函数和 lambda 返回 false?

有如下一段代码:#include#includetemplate::value>::type>intfun(Ff)//line8{returnf(3);}intl7(intx){returnx%7;}intmain(){autol=[](intx)->int{returnx%7;};fun(l);//line23//fun(l7);thiswillalsofaileventhoughl7isaregularfunctionstd::cout::value;//prints1}我会得到以下错误:main2.cpp:Infunction‘intmain()’:main2.cpp:23:8:

c++ - 开关盒 : declaration-with-initialization & declaration-and-then-assignment

在switch-case语句中,declaration-with-initialization是无效的,但允许declaration-and-then-assignment。如以下代码片段所示。从编译器端看,这两种类型的初始化有什么区别?以及为什么第一种初始化无效而第二种初始化有效。switch(val){case0:intnewVal=42;//Invalidbreak;case1:intnewVal2;//ValidnewVal2=42;break;case2:break;} 最佳答案 实际上,规则是您不能跳入经过具有初始化的声

c++ - std::function 在移动后是否重置其内部功能

#includeusingnamespacestd;#includetemplateclassScopeExitFunction{public:ScopeExitFunction(F&func)throw():m_func(func){}ScopeExitFunction(F&&func)throw():m_func(std::move(func)){}ScopeExitFunction(ScopeExitFunction&&other)throw():m_func(std::move(other.m_func)){//other.m_func=[]{};}~ScopeExitFunc

C++ OOP : Which functions to put into the class?

假设我有一个类a:classa{public:voidload_data();private:voidcheck_data();voidwork_data();voidanalyze_data();}这些函数都对类或其成员做一些事情。但是这个函数:boolvalidate_something(myTypemyData){if(myData.blah>0&&myData.blah与类相关,只会被它调用,所以其他地方都不需要它不对类或其成员做任何事情——只是一个小的“实用”函数在哪里放置validate_something?课内还是课外? 最佳答案

C++ 编译器错误 "was not declared in this scope"

我在尝试编译C++UDP客户端程序时遇到奇怪的编译器错误。g++-oclientUdp.cppClientMain.c-I.-lpthreadInfileincludedfromClientMain.c:1:0:Udp.h:Indestructor‘CUdpMsg::~CUdpMsg()’:Udp.h:103:43:error:‘free’wasnotdeclaredinthisscopeUdp.h:Inmemberfunction‘voidCUdpMsg::Add(in_addr_t,constvoid*,size_t)’:Udp.h:109:34:error:‘malloc’was

c++ - std::function 的解释

std::function的目的是什么?据我所知,std::function将函数、仿函数或lambda转换为函数对象。我不太明白这样做的目的...Lambdas和Functors都已经是函数对象,我相信它们可以用作排序和转换等算法的谓词。作为旁注,Lambda实际上是仿函数(内部)。所以我唯一能看到std::function有用的是将常规函数转换为函数对象。而且我也不明白为什么要将常规函数转换为函数对象。如果我想使用一个函数对象,我会首先将一个作为仿函数或lambda...而不是编写一个函数,然后用std::function转换它,然后将它作为谓词传递...我猜std::functi