草庐IT

non-function

全部标签

c++推导 "non type pointer to function"类模板参数

考虑一个模板类:templateclassProxy{voidrun(){ReturnTyperet=Fn();//...dosomething...}};//andafunctionsintfn1(){return5;}floatfn2(){return5;}这可以通过使用实例化:Proxyp1;但是显式声明返回值类型似乎是不必要的。我想要实现的是:someProxyInstantationp1;someProxyInstantationp2;不幸的是,我对c++没有任何期望,这似乎是该语言的一个隐藏角落(至少对我而言)。如果我可以从指向函数的指针获取它的类型——类似于:std::t

c++ - std::binary_function - 调用不匹配?

包括#includeusingnamespacestd;intmain(){binary_functionoperations[]={plus(),minus(),multiplies(),divides()};doublea,b;intchoice;cout>a>>b;cout>choice;cout我得到的错误是:Calcy.cpp:Infunction‘intmain()’:Calcy.cpp:17:error:nomatchforcallto‘(std::binary_function)(double&,double&)’谁能解释为什么我会收到此错误以及如何消除它?

C++ 模板和 "no matching function to call"

我遇到了一个奇怪的错误。我有以下签名的功能:templatestaticboolConvertCbYCrYToRGB(constCharacteristicspace,constDATA*input,DATA*output,constintpixels){后来这样称呼:casekByte:returnConvertCbYCrYToRGB(space,(constU8*)input,(U8*)output,pixels);casekWord:returnConvertCbYCrYToRGB(space,(constU16*)input,(U16*)output,pixels);casek

c++ - begin() 如何知道要返回哪种返回类型(const 或 non-const)?

这非常有效:listl;list::const_iteratorit;it=l.begin();list::iteratorit2;it2=l.begin();我不明白list是怎么来的“知道”它必须返回iteratorbegin()版本或const_iteratorbegin()const一个。我正在尝试为我的容器(一个trie)实现迭代器,但我遇到了这个问题。难道C++不应该不按返回类型处理差异化(除非使用奇怪的技巧)?这是一些代码和我得到的编译器错误:我的Trie是一个模板化的trie,可以包含任何类型。我有一个Trie::iter非常量迭代器和一个Trie::const_ite

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++ - 'non-virtual interface' 和 'abstract interface' 有什么区别?

我正在用C++实现设计模式,我希望我的类通过组合来利用接口(interface),这让我研究了实现接口(interface)的不同方法。我想澄清一下这个术语的定义。 最佳答案 非虚拟接口(interface)是一个公共(public)成员函数,它不是虚拟的,但通常希望根据可覆盖的虚拟函数来实现:classInterface{public:intcompute(){returncompute_impl();}private:virtualintcompute_impl()=0;protected:virtual~Interface()

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如果我不必指定非常麻烦的占位符,它会适合我。所以我尝试另一种方法。我为成员方法添加了一个新的