草庐IT

override_function

全部标签

c++ - 重载 "function call"运算符有什么用处?

我最近发现,在C++中你可以重载“函数调用”操作符,以一种奇怪的方式,你必须编写两对括号来这样做:classA{intn;public:voidoperator()()const;};然后这样使用:Aa;a();什么时候有用? 最佳答案 这可用于创建"functors",像函数一样工作的对象:classMultiplier{public:Multiplier(intm):multiplier(m){}intoperator()(intx){returnmultiplier*x;}private:intmultiplier;};Mul

c++ - 元编程 : Failure of Function Definition Defines a Separate Function

在thisanswer我根据类型的is_arithmetic属性定义了一个模板:templateenable_if_t::value,string>stringify(Tt){returnto_string(t);}templateenable_if_t::value,string>stringify(Tt){returnstatic_cast(ostringstream()dypsuggests而不是类型的is_arithmetic属性,是否为类型定义to_string是模板选择标准。这显然是可取的,但我不知道怎么说:Ifstd::to_stringisnotdefinedthenu

c++ - 从 std::binary_function (或 std::unary 函数)继承有什么好处?

从std::binary_function(或std::unary_function)继承有什么好处?例如我有这样的代码:classPerson{public:Person();Person(inta,std::stringn);Person(constPerson&src);intage;std::stringname;};Person::Person():age(0),name(""){};Person::Person(inta,std::stringn):age(a),name(n){};Person::Person(constPerson&src){age=src.age;na

c++ - Gold 所描述的 C++ "Key Function"是什么?

请不要回答“我该如何解决这个错误信息?”的问题在gold提供的错误信息中:/usr/bin/ld.gold:thevtablesymbolmaybeundefinedbecausetheclassismissingitskeyfunction什么是按键功能?我在GCCmanualpageforFunctionAttributes中找到了它的引用。在dllimport部分下。相关文字如下:OntheSHSymbianOStargetthedllimportattributealsohasanotheraffect(sic)—itcancausethevtableandrun-timety

c++ - How to get around GCC ‘*((void*)& b +4)’ may be used uninitialized in this function warning while using boost::optional

我有类似下面的代码:#include::boost::optionalgetitem();intgo(intnr){boost::optionala=getitem();boost::optionalb;if(nr>0)b=nr;if(a!=b)return1;return0;}当使用GCC4.7.2和Boost1.53进行编译时,使用以下命令:g++-c-O2-Wall-DNDEBUG发出以下警告:13:3:warning:‘((void)&b+4)’maybeuseduninitializedinthisfunction[-Wmaybe-uninitialized]显然,根本问题在

c++ - 重载分辨率和数组 : which function should be called?

考虑以下程序:#include#includevoidf(charconst*&&){std::puts("charconst*&&");}//(1)voidf(charconst*const&){std::puts("charconst*const&");}//(2)templatevoidf(charconst(&)[N]){std::puts("charconst(&)[N]");}//(3)intmain(){constchardata[]="a";f(data);}应该调用哪个f?为什么?三个编译器的最新发布版本对这个问题的答案存在分歧:(1)在使用g++4.5.2编译程序时

c++ - std::function 到成员函数

#includestructA{intfunc(intx,inty){returnx+y;}};intmain(){typedefstd::functionFuncp;Aa;//Funcpfunc=std:::bind(&A::func,&a);Funcpfunc=std::bind(&A::func,a,std::placeholders::_1);return0;}我在上述两个绑定(bind)函数中都遇到了错误:errorC2825:'_Fty':mustbeaclassornamespacewhenfollowedby'::'语法错误在哪里?我正在使用VisualStudio20

c++ - 回调(std::function/std::bind)与接口(interface)(抽象类)的优缺点

我正在使用Boost.Asio在C++11中创建一个服务器应用程序。我创建了一个类,Server,它负责接受新的连接。基本上就是这样:voidServer::Accept(){socket_.reset(newboost::asio::ip::tcp::socket(*io_service_));acceptor_.async_accept(*socket_,boost::bind(&Server::HandleAccept,this,boost::asio::placeholders::error));}voidServer::HandleAccept(constboost::sys

c++ - VS 2015编译cocos2d-x 3.3报错 "fatal error C1189: #error: Macro definition of snprintf conflicts with Standard Library function declaration"

当我使用visualstudio2015编译cocos2d-x(3.3版)时,出现错误,说:fatalerrorC1189:#error:MacrodefinitionofsnprintfconflictswithStandardLibraryfunctiondeclaration(编译源文件..\base\s3tc.cpp)源码为:#ifdefsnprintf#errorMacrodefinitionofsnprintfconflictswithStandardLibraryfunctiondeclaration#endif谁能告诉我怎么了? 最佳答案

c++ - std::function 可以存储指向数据成员的指针吗?

来自cppreference,我发现:Classtemplatestd::functionisageneral-purposepolymorphicfunctionwrapper.Instancesofstd::functioncanstore,copy,andinvokeanyCallabletarget--functions,lambdaexpressions,bindexpressions,orotherfunctionobjects,aswellaspointerstomemberfunctionsandpointerstodatamembers.我不明白为什么std::fun