草庐IT

load_functions

全部标签

c++ - 如何使用 lambda 在 std::function 参数中推导模板类型?

我有一个boost::variant并且我只想在变体是特殊类型时才执行一个仿函数,所以我编写了这个函数:templatevoidif_init(Variant&opt_variant,std::functionfunctor){if(auto*ptr=boost::get(&opt_variant)){functor(*ptr);}}这很好用,但我希望推导出类型T,这样我就可以这样写:if_init(b,[](doublevar){std::cout但是没有推导出类型:type_inference.cpp:19:5:error:nomatchingfunctionforcallto'i

c++ - 将抽象仿函数分配给 std::function - 为什么 std::ref 是一个解决方案?

我想将仿函数对std::function的赋值封装到一个方法中。我必须传递从通用抽象类Slot继承的仿函数,而不是传递std::function或指向std::function的指针(即这些槽提供额外的功能)。我以不同的形式偶然发现了这个问题here.例如。在那里,使用通用槽指针而不是std:functions的动机是仿函数的生命周期管理。下面的代码说明了这个问题。请参阅assignFunctorPtr(...)方法。#include#includetemplateclassSlot;templateclassSlot{public:typedefRRet_type;public:vi

c++ - 显式默认和删除的构造函数 : is there any similar functionality available in VS2012?

在VS2012中,“显式默认和删除特殊成员函数”功能(http://en.wikipedia.org/wiki/C++0x#Explicitly_defaulted_and_deleted_special_member_functions、http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm)尚不可用(http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx)。是否有任何解决方法来使用此类功能,即使非常冗长?在实践中,我可以翻译这个吗struc

c++ - 错误 : no instance of overloaded function "std::make_shared" matches the argument list

查看ApreviousstackQuestionstd:make_sharedvsstd::shared_ptr,我试图在一个uni项目中实现它。这是之前的“问题”:Ican'tthinkofanysituationwherestd::shared_ptrobj(newObject("foo",1));wouldbepreferredtoautoobj=std::make_shared("foo",1);因此我采用了这段代码:std::shared_ptrpT1(newTriangle(pCanvas,30,30,30,60,60,30,255,0,0));并将其修改为这段代码:aut

c++ - 使用尽可能少的代码将非静态方法包装到带有 "this"参数绑定(bind)的 std::function

这是我正在尝试做的事情:templateclassCSignal{public:voidconnect(std::functiontarget){m_connections.emplace_back(target);}private:mutablestd::vector>m_connections;};connect非常适合静态方法或全局函数。现在,如果我想传递一个成员方法怎么办?看来这是我唯一的选择:structMyStruct{voidprint(floata,intb){std::cout如果我不必指定非常麻烦的占位符,它会适合我。所以我尝试另一种方法。我为成员方法添加了一个新的

c++ - 在嵌套的 lambda 中应用 function_traits 时编译失败

首先,我有这样的东西,一个重命名的function_traits来获取lambda的返回类型templatestructFuncAnalyzer{};templatestructFuncAnalyzer{usingTReturn=TRet;};templatestructFunctionAnalyzer:publicFuncAnalyzer{};然后当我在一个方法中有这个时,那个compi:autoa=[](constint&key)->QString{returnQString::number(key);};usingb=FunctionAnalyzer::TReturn;bx;但是

c++ - Qt : Display a picture during application loading

我想为加载缓慢的应用程序添加启动画面。我已经创建了一个简单的应用程序来测试。main.cpp:intmain(intargc,char*argv[]){QApplicationapp(argc,argv);QPixmappixmap("/home/helene/Images/my_image.png");if(pixmap.isNull()){pixmap=QPixmap(300,300);pixmap.fill(Qt::magenta);}QSplashScreen*splash=newQSplashScreen(pixmap);splash->show();splash->show

c++ - "unresolved overloaded function type"在 std::function 中有静态函数

尝试将重载静态函数传递给std::function时出现“未解析的重载函数类型”错误。我知道类似的问题,例如this和this.然而,即使那里的答案可以将正确函数的地址放入函数指针中,它们也会因std::function而失败。这是我的MWE:#include#include#includestructClassA{staticstd::stringDoCompress(conststd::string&s){returns;}staticstd::stringDoCompress(constchar*c,size_ts){returnstd::string(c,s);}};voidh

c++ - 在调用之前检查 std::function 的有效性?

我正在尝试编写一个简单但灵活的事件系统(主要是作为练习,我知道现有的库具有非常好的事件处理程序),但我遇到了一个小绊脚石。如何检查作为委托(delegate)的std::function(可能通过lambda,可能通过std::bind)是否为有效函数/成员函数的对象是否仍然存在在打电话之前?我试过简单地使用std::function的bool运算符,但没有取得任何成功。理想情况下,我希望A.在委托(delegate)函数内部以外的地方进行检查,并且B.当被检查的std::function不是委托(delegate)时,代码仍然有效。有什么想法吗?编辑:这是我运行的测试的源代码#inc

c++ - 什么是非时间流加载固有 (_mm256_stream_load_si256) 的浮点 (__m256d) 版本?

在AVX/AVX2中我只能找到_mm256_stream_load_si256(),用于__m256i。没有办法流式加载__m256d吗?为什么?(我想在不污染CPU缓存的情况下加载它)做下面的(aggressivecasting)有什么障碍吗?__m256d*pDest=/*...*/;__m256d*pSrc=/*...*/;/*...*/const__m256iiWeight=_mm256_stream_load_si256(reinterpret_cast(pSrc));const__m256dprior=_mm256_div_pd(*reinterpret_cast(&iWe