草庐IT

member-functions

全部标签

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++ - 为什么静态成员函数只有在有返回值的情况下才能在全局范围内调用?

我发现了一个奇怪的事情:类/结构的静态成员函数不能被称为全局作用域,除非它们有返回值。这个程序不编译:structtest{staticvoiddostuff(){std::cout在GCCv4.8.3下为我们提供以下内容:main.cpp:12:16:error:expectedconstructor,destructor,ortypeconversionbefore';'tokentest::dostuff();^但是,通过将返回值添加到dostuff()并将其分配给全局变量,程序可以按预期编译和工作:structtest{staticintdostuff(){std::cout这

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++ - 为什么这些方法调用不明确?

#includeusingString=std::string;classBase{protected:Stringvalue;};classReadonly:virtualBase{public:constString&method()const{returnvalue;}String&method(){returnvalue;}};classWriteonly:virtualBase{public:Writeonly&method(constString&value){this->value=value;return*this;}Writeonly&method(String&&v

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

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

c++ - 哪个更好 : returning tuple or passing arguments to function as references?

我创建的代码中有两个函数returnValues和returnValuesVoid。一个返回2个值的元组,另一个接受参数对函数的引用。#include#includestd::tuplereturnValues(constinta,constintb){returnstd::tuple(a,b);}voidreturnValuesVoid(int&a,int&b){a+=100;b+=100;}intmain(){auto[x,y]=returnValues(10,20);std::cout我读到了http://en.cppreference.com/w/cpp/language/st

c++ - 如何使用函数签名的 typedef 作为 std::function 的类型参数?

当用作模板类型参数时,函数的typedef和使用裸函数类型之间肯定有区别。即,考虑#includetypedefstd::functionTF1;typedefvoid(*FooFn)(int);typedefstd::functionTF2;intmain(){TF1tf1;TF2tf2;return0;}我可以创建TF1但不能创建TF2(错误:aggregate'TF2tf2'的类型不完整,无法定义)。(参见ideoneexample。)有没有办法使用函数(签名)的typedef作为模板类型参数;具体来说,作为std::function?的类型参数(没有C++11标记,因为我对bo

c++ - 成员函数模板放在哪里

定期让我感到沮丧的C++方面是决定模板在头文件(传统上描述接口(interface))和实现(.cpp)文件之间的位置。模板通常需要放在header中,公开实现,有时还需要引入额外的header,而这些header以前只需要包含在.cpp文件中。我最近又遇到了这个问题,下面是一个简化的例子。#include//for~Counter()andcountAndPrint()classCounter{unsignedintcount_;public:Counter():count_(0){}virtual~Counter();templatevoidcountAndPrint(constT