草庐IT

boost-function

全部标签

c++ - std::function 签名指针 vs 指针引用 = 没有区别?

这是代码示例:#include#includestructFoo{};typedefboolfunc_type(Foo*&,conststd::string&);typedefstd::functionFunctionalType;boolf(Foo*,conststd::string&){}intmain(){#if1func_type*func;func=f;#elseFunctionalTypef2;f2=f;#endif}如您所见,我已将“对指针的引用”声明为第一个参数的函数类型Foo*&,我希望该函数仅以“指针”作为第一个参数Foo*不能分配给这种类型的变量。#if1regi

c++ - boost::spirit 解析为 fusion 适应结构可选但独占

如果有一个结构:structrecord{std::stringtype;std::stringdelimiter;uint32_tlength;std::stringname;record(){type="";delimiter="";length=0;name="";}};使用boost::fusion和以下语法进行改编:structrecord_parser:qi::grammar{record_parser():record_parser::base_type(start){usingqi::lit;usingqi::uint_;usingqi::lexeme;usingasc

c++ - 使用 boost::program_options 禁止无符号值的负参数

假设我有一个程序使用boost::program_options来解析命令行参数,其中一个有一个unsigned值:#include#includenamespacepo=boost::program_options;intmain(intargc,char*argv[]){unsignednum;po::options_descriptiondesc;desc.add_options()("num,n",po::value(&num),"Non-negativenumber");po::variables_mapvm;po::store(po::parse_command_line(

c++ - Boost.Hana 中是否有与 Boost.Fusion 中的 View 概念等效的概念?

我尝试使用hana::for_each迭代用户定义的结构,并注意到它被复制/移动,而Boost.Fusion允许您迭代在原始结构上。我没有在Boost.Hana中找到任何类似于Boost.Fusion的View概念。如何将转换应用于序列而不每次都复制/移动它们?#include#includestructFoo{Foo()=default;Foo(constFoo&){std::cout更新:我尝试使用hana::transform将std::ref应用于成员,但是Struct不是Functior,所以transform不适用于这种情况。我能够使用hana::accessors实现所需

c++ - 将 boost ptime 从本地时间转换为 UTC

我有一个boost::posix_time::ptime对象(Boostv1.60),它包含系统时区中的日期和时间信息。我需要将其转换为UTC格式的unix时间戳。time_tconvertLocalPtimeToTimestamp(constboost::posix_time::ptime&pt){usingnamespaceboost::local_time;staticconsttime_tt_null=0;staticstructtm*tm_local=localtime(&t_null);statictime_zone_ptrzone(newposix_time_zone(t

c++ - 使用 std::function 或转发引用作为高阶函数的通用可调用对象输入参数?

我想知道编写一个以std::function作为输入参数的高阶函数的主要区别、优缺点。或转发引用,例如templatevoidhof(F&&fun);.显然,前者比后者更严格,因为它指定了输入可调用对象必须符合的函数类型。 最佳答案 std::function通常具有显着的运行时开销。通过template参数传递通用可调用对象可避免std::function的间接成本,并允许编译器积极优化。我在theendofthisarticle为lambda递归编写了一些简单的基准测试(Y组合器与std::function).std::func

c++ - 使用 boost asio 捕捉 Ctrl-C

我正在尝试在应用程序中捕获Ctrl-C,如下面的MWE所示#include#includevoidhandler(constboost::system::error_code&error,intsignal_number){std::cout>choice;}}不幸的是,当我按下Ctrl+C时,handler()没有被调用。相反,循环不再等待用户输入,如下所示:c:\tmp>CtrlC.exePressakey:dPressakey:ePressakey:Pressakey:Pressakey:Pressakey:Pressakey:Pressakey:Pressakey:Pressa

c++ - 模板实例化失败 : compiler choosing improper overload function

我对模板并不陌生,但我遇到了一个相当奇怪的问题,我需要将模板类型分离到它的组件中,以用于我正在处理的数据序列化程序。这很难解释,所以我已经证明了。这是我简化的示例问题,example.cpp。templatevoidfoo(T&arg){}templatevoidfoo(T&arg){}intmain(intargc,char*argv[]){foo(argc);return0;}我得到一个错误,然后是一个警告,这似乎表明它正在尝试实例化两个函数,但只有其中一个是合适的。$g++-Wall-Wexample.cppexample.cpp:2:43:error:‘T’isnotatemp

c++ - syntax::function_name 在 C++ 中是什么意思?

在名为::foo()的函数中,我不明白语法的用途。如果它是foo::count_all()那么我知道count_all是类或命名空间foo的函数。在::foo()的情况下,::引用的是什么? 最佳答案 ::运算符正在调用namespace或class。在您的情况下,它正在调用全局命名空间,它是不在命名空间中的所有内容。下面的例子说明了为什么namespace很重要。如果您只是调用foo(),您的调用将无法解析,因为有2个foo。您需要使用::foo()解决全局问题。namespaceHidden{intfoo();}intfoo()

c++ - 错误 : no matching member function for call to 'push_back'

为什么我在最后两行收到错误?目标是在集合中找到对象,并修改其内容。usingnamespacestd;structmystruct{intid;vectory;mystruct(constintid):id(id){}booloperatorsx;mystructx(1);x.y.push_back(1);x.y.push_back(2);sx.insert(x);//set::iteratori=sx.find(1);constmystruct*x1=&(*i);constmystructx2=*x1;couty)y)y.push_back(4);}好像迭代器返回的是常量对象,不让我