草庐IT

boost-lambda

全部标签

c++ - boost 中的 path::string() 和 path::generic_string() 有什么区别?

boost::path::string()和boost::path::generic_string()有什么区别,我应该什么时候使用它们? 最佳答案 这在thedocumentation中有明确说明;您只需阅读文档即可获得知识和理解。请从现在开始养成这样做的习惯。boost::路径::字符串在thenativepathnameformat中返回一个std::string.boost::path::generic_string在thegenericpathnameformat中返回一个std::string.何时使用它们中的每一个好吧

c++ - boost 结构和类的 fusion 序列类型和名称识别

我正在尝试为我的一个项目使用boostfusion,并且我正在弄清楚如何获取结构和类的类型名称和变量名称。#include#include#include#include#include#include#include#include#includeusingnamespaceboost::fusion;structFoo{intinteger_value;boolboolean_value;};classBar{intinteger_value;boolboolean_value;public:Bar(inti_val,boolb_val):integer_value(i_val),

c++ - 返回 Boost Graph 中连接的组件子图的列表

我在过滤原始图中具有相同组件的子图时遇到问题。我想将它们输出到子图的vector中。按照`connected_components中的示例,我尝试使其适应我的需要://CreateatypedeffortheGraphtypetypedefadjacency_list,property>Graph;//typedefsubgraphSubGraph;typedeftypenamegraph_traits::vertex_descriptorVertex;typedeftypenamegraph_traits::edge_descriptorEdge;typedefgraph_trait

c++ - boost::可选抽象类类型

我想要一个抽象类型的可选对象。然而,boost::optional对于抽象类型是失败的:#includeclassA{virtualintgetInt()=0;};classB:publicA{intgetInt(){return666;};};intmain(){boost::optionalmaybeAnA;boost::optionalanother(maybeAnA);};结果error:invalidnew-expressionofabstractclasstype‘A’只使用指针似乎也不是一个可行的解决方案,因为某些函数需要返回一个包含可选值的对象作为成员,尽管期望函数的调

c++ - Lambda 重新初始化 vector - 为什么它有效?

为什么下面的编译?vectorvec;autolambda=[vec(move(vec))](){//??};如何使用vec(move(vec))重新初始化已分配的vec变量?这不是调用移动构造函数吗?如果我写:vectorvec;vec(move(vec));这是无效的 最佳答案 这称为init-capture。它声明了一个新变量,它隐藏了上面的vec。它用于捕获lambda表达式中的仅移动类型:Aninit-capturebehavesasifitdeclaresandexplicitlycapturesavariableoft

c++ - 将 boost::log::expressions::attr< std::string > 转换为 std::string

在使用Boost.Log时,我试图保留我的TimeStamp格式化程序,例如:logging::add_file_log(keywords::file_name="my.log",keywords::format=(expr::stream("TimeStamp","%Y-%m-%d%H:%M:%S")("Line")("File")据说我不能使用其他形式的格式化程序,因为我将很难转换"TimeStamp"转换成我的自定义格式:staticvoidmy_formatter(logging::record_viewconst&rec,logging::formatting_ostream

c++ - 是否可以显式特化模板以匹配 lambda?

假设我有一个headerwrapper.h:templatevoidwrapper(constFuncfunc);和一个文件wrapper.cpp包含:#include"wrapper.h"templatevoidwrapper(constFuncfunc){func();}还有一个文件main.cpp包含:#include"wrapper.h"#includeintmain(){wrapper([](){std::cout如果我将这些一起编译(例如,catwrapper.cppmain.cpp|g++-std=c++11-omain-xc++-),我没有收到链接器错误。但是如果我单独

局部变量的 C++ lambda 词法闭包

总结在C++中,当我从捕获该函数局部变量的函数返回lambda时,具体会发生什么,为什么?编译器(g++)似乎允许这样做,但它给我的结果与我预期的不同,所以我不确定这在技术上是否安全/受支持。详情在某些语言(Swift、Lisp等)中,您可以在闭包/lambda中捕获局部变量,只要闭包在范围内,它们就保持有效并在范围内(我听说它称为“lambdaover在Lisp上下文中放弃lambda”)。例如,在Swift中,我尝试做的示例代码是:funccounter(initial:Int)->(()->Int){varcount=initialreturn{count+=1;returnco

c++ - 在递归 lambda 中按值捕获

我们可以像这样定义递归lambda函数std::functionfun=[&fun](inta){if(a)fun(a-1);};然后我们可以调用它fun(10);但是,如果我将定义更改为std::functionfun=[fun](inta){if(a)fun(a-1);};然后尝试调用fun(10);发生段错误。谁能解释一下为什么按引用捕获有效,而按值捕获会导致段错误。 最佳答案 按值捕获作为评估lambda表达式的一部分进行评估。那时,fun仍未初始化,因为您仍在评估其初始化程序。只有在那之后fun才被初始化,但那时复制已经发

c++ - 如何将 `boost::static_visitor` 实例传递给函数

我正在使用boost::variant在我的项目中经常出现。我的同事们现在想出了传递特定boost::static_visitor实例的想法。以自定义访问类型。她有一些代码如下:#include#includetypedefboost::variantTVar;structVisitor1:publicboost::static_visitor{templateresult_typeoperator()(constT&){return42;}};structVisitor2:publicboost::static_visitor{templateresult_typeoperator(