草庐IT

defaulted-functions

全部标签

c++ - 为什么 std::function 实例有默认构造函数?

这可能是一个哲学问题,但我遇到了以下问题:如果你定义了一个std::function,但你没有正确初始化它,你的应用程序将会崩溃,像这样:typedefstd::functionMyFunctionType;MyFunctionTypemyFunction;myFunction();如果函数作为参数传递,像这样:voidDoSomething(MyFunctionTypemyFunction){myFunction();}然后,当然,它也会崩溃。这意味着我不得不添加这样的检查代码:voidDoSomething(MyFunctionTypemyFunction){if(!myFunct

c++ - 使用 std::function 移动语义

std::function从右值引用提供构造函数。按照标准,移动的函数对象会发生什么?会不会是空的,所以再次调用它没有效果? 最佳答案 围绕这个问题有太多的困惑。我会尽量把事情说清楚...本节描述标准定义对象的移出状态:17.6.5.15[lib.types.movedfrom]ObjectsoftypesdefinedintheC++standardlibrarymaybemovedfrom(12.8).Moveoperationsmaybeexplicitlyspecifiedorimplicitlygenerated.Unle

c++ - 使用 std::function 移动语义

std::function从右值引用提供构造函数。按照标准,移动的函数对象会发生什么?会不会是空的,所以再次调用它没有效果? 最佳答案 围绕这个问题有太多的困惑。我会尽量把事情说清楚...本节描述标准定义对象的移出状态:17.6.5.15[lib.types.movedfrom]ObjectsoftypesdefinedintheC++standardlibrarymaybemovedfrom(12.8).Moveoperationsmaybeexplicitlyspecifiedorimplicitlygenerated.Unle

c++ - OpenCV GTK+2.x 错误 - "Unspecified error (The function is not implemented...)"

我已经按照这些步骤安装了OpenCV。在尝试编译一个示例后,我得到了这个错误:OpenCVError:Unspecifiederror(Thefunctionisnotimplemented.RebuildthelibrarywithWindows,GTK+2.xorCarbonsupport.IfyouareonUbuntuorDebian,installlibgtk2.0-devandpkg-config,thenre-runcmakeorconfigurescript)incvNamedWindow,file/home/nick/.Apps/opencv/modules/high

c++ - OpenCV GTK+2.x 错误 - "Unspecified error (The function is not implemented...)"

我已经按照这些步骤安装了OpenCV。在尝试编译一个示例后,我得到了这个错误:OpenCVError:Unspecifiederror(Thefunctionisnotimplemented.RebuildthelibrarywithWindows,GTK+2.xorCarbonsupport.IfyouareonUbuntuorDebian,installlibgtk2.0-devandpkg-config,thenre-runcmakeorconfigurescript)incvNamedWindow,file/home/nick/.Apps/opencv/modules/high

c++ - std::function<> 的 vector

说要存储以下内容:typedefstd::functionMyFunctionDecl;..在一个集合中:typedefstd::vectorFunctionVector;FunctionVectorv;这是可能的,但如果我想find使用std::find:FunctionVector::const_iteratorcit=std::find(v.begin(),v.end(),myFunctionDecl);..由于==运算符,我们得到一个错误。正如在上一个问题中向我建议的那样,这可以通过将函数声明封装在另一个类中来解决,该类提供==运算符:classWrapper{private:

c++ - std::function<> 的 vector

说要存储以下内容:typedefstd::functionMyFunctionDecl;..在一个集合中:typedefstd::vectorFunctionVector;FunctionVectorv;这是可能的,但如果我想find使用std::find:FunctionVector::const_iteratorcit=std::find(v.begin(),v.end(),myFunctionDecl);..由于==运算符,我们得到一个错误。正如在上一个问题中向我建议的那样,这可以通过将函数声明封装在另一个类中来解决,该类提供==运算符:classWrapper{private:

C++ 11 : is a defaulted copy constructor user declared?

我猜是这样,但我正在寻找C++11语言律师来确认我的印象。下面的课是真的吗structX{X(){}X(Xconst&)=default;};不会自动启用移动,即获取X(X&&)和operator=(X&&),因为它的复制构造函数是“用户声明的”,即使它看起来等同于structX{};这将获得X(Xconst&)和X(X&&)等,在使用时隐式声明和(平凡)定义。 最佳答案 来自标准:8.4.2Explicitly-defaultedfunctions[dcl.fct.def.default]4-[...]Aspecialmember

C++ 11 : is a defaulted copy constructor user declared?

我猜是这样,但我正在寻找C++11语言律师来确认我的印象。下面的课是真的吗structX{X(){}X(Xconst&)=default;};不会自动启用移动,即获取X(X&&)和operator=(X&&),因为它的复制构造函数是“用户声明的”,即使它看起来等同于structX{};这将获得X(Xconst&)和X(X&&)等,在使用时隐式声明和(平凡)定义。 最佳答案 来自标准:8.4.2Explicitly-defaultedfunctions[dcl.fct.def.default]4-[...]Aspecialmember

c++ - 返回没有 std::function 的 lambda

考虑以下返回lambda的函数:std::functionmake_counter(){inti=0;return[=]()mutable{returni++;};}是否可以返回实际的lambda类型,而不将其包装到std::function中? 最佳答案 C++11:不。每个lambda表达式都有,我引用(§5.1.2/3):[...]aunique,unnamednon-unionclasstype[...]这实际上意味着如果不先知道相应的表达式,就无法知道lambda的类型。现在,如果您没有捕获任何内容,您可以使用转换为函数指