草庐IT

override_function

全部标签

c++ - 我不能将 lambda 作为 std::function 传递

让我们专注于这个例子:templateclassC{public:voidfunc(std::vector&vec,std::function&f){//DoSomething}};现在,我正在尝试:std::vectorvec;autolambda=[](conststd::string&s){returnstd::stoi(s);};Cc;c.func(vec,lambda);它会导致错误:nomatchingfunctionforcallto‘C::func(std::vector>&,main()::&)’ref.parse(vec,lambda);请解释一下什么是不正确的以及

c++ - 可选功能参数 : Use default arguments (NULL) or overload the function?

我有一个处理给定vector的函数,但如果没有给出,也可以自己创建这样的vector。对于这种情况,我看到了两种设计选择,其中函数参数是可选的:将其设为指针,默认设为NULL:voidfoo(inti,std::vector*optional=NULL){if(optional==NULL){optional=newstd::vector();//fillvectorwithdata}//processvector}或者有两个具有重载名称的函数,其中一个省略了参数:voidfoo(inti){std::vectorvec;//fillvecwithdatafoo(i,vec);}voi

c++ - 覆盖纯虚函数时使用 `override` 有什么意义吗?

例如:classBase{virtualvoidmy_function()=0;};classDerived:Base{voidmy_function()override;};根据我的阅读,override关键字用于确保我们在要覆盖的函数中具有正确的签名,并且这似乎是它的唯一用途。但是,在纯虚函数的情况下,如果我们在派生类(或基类,取决于人们如何看待事物)中使用了不正确的签名,编译器会抛出错误。那么,在Derived::my_function()声明的末尾添加override有什么意义吗? 最佳答案 However,inthecas

c++ - 指定 std::function 的默认值

voidfunc(conststd::function&f=empty){if(f)f();}“空”应该是什么?我使用[](){}。但从技术上讲,那不是空的,f()会执行。 最佳答案 voidfunc(conststd::function&f={}){if(f)f();}LIVEDEMO 关于c++-指定std::function的默认值,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questio

c++ - 如何从移动捕获 lambda 表达式创建 std::function?

我正在尝试创建一个std::function来自移动捕获lambda表达式。请注意,我可以毫无问题地创建一个移动捕获lambda表达式;只有当我尝试将它包装在std::function中时我得到一个错误。例如:autopi=std::make_unique(0);//noproblemshere!autofoo=[q=std::move(pi)]{*q=5;std::coutbar=foo;std::functionbar{foo};std::functionbar{std::move(foo)};std::functionbar=std::move(foo);std::functio

c++ - std::function 的仅 move 版本

因为std::function是可复制的,标准要求用于构造它的可调用对象也是可复制的:n337(20.8.11.2.1)templatefunction(Ff);Requires:FshallbeCopyConstructible.fshallbeCallable(20.8.11.2)forargumenttypesArgTypesandreturntypeR.ThecopyconstructoranddestructorofAshallnotthrowexceptions.`这意味着不可能形成std::function来自不可复制的绑定(bind)对象或捕获仅move类型的lambd

c++ - 如何强制执行 'override' 关键字?

有没有办法强制在VisualC++2012中使用C++11override关键字?(即,如果我忘记说override,那么我想得到一个警告/错误。) 最佳答案 C++11几乎拥有你想要的。最初,override关键字是一个更大的提案(N2928)的一部分,其中还包括强制使用它的能力:classA{virtualvoidf();};classB[[base_check]]:publicA{voidf();//error!};classC[[base_check]]:publicA{voidf[[override]]();//OK};b

C++ 错误 'Undefined reference to Class::Function()'

这个问题在这里已经有了答案:Whatisanundefinedreference/unresolvedexternalsymbolerrorandhowdoIfixit?(38个回答)关闭8年前。我想知道是否有人可以帮助我解决这个问题-我只是C++新手,这给我带来了很多麻烦。我正在尝试制作相对简单的Deck和Card类对象。错误出现在“Deck.cpp”中,声明了一组卡片,然后当我尝试用卡片对象填充数组时。它表示对Card::Card()、Card::Card(Card::Rank,Card::Suit)和Card::的引用未定义~Card().我的所有包含似乎都是正确的,所以我不知道

当涉及 std::function 或 lambda 函数时,C++11 不推断类型

当我定义这个函数时,templatesettest(constset&input){returninput;}我可以使用test(mySet)调用它代码中的其他地方,而不必显式定义模板类型。但是,当我使用以下功能时:templatesetfilter(constset&input,functioncompare){setret;for(autoit=input.begin();it!=input.end();it++){if(compare(*it)){ret.insert(*it);}}returnret;}当我使用filter(mySet,[](inti){returni%2==0

c++ - Eric Niebler 的 std::is_function 实现如何工作?

上周埃里克·尼布勒tweetedstd::is_function的非常紧凑的实现特质类:#includetemplatestructpriority_tag:priority_tag{};templatestructpriority_tag{};//Functiontypeshere:templatechar(&is_function_impl_(priority_tag))[1];//Arraytypeshere:templatechar(&is_function_impl_(priority_tag))[2];//Anythingthatcanbereturnedfromafunc