使用 boost lambda 访问静态成员
我正在尝试使用 boost::lambda 编写一些简单的谓词,但遇到了大量错误。
我检查了文档,对访问 lambda 表达式中的静态变量 std::string::npos
有一些疑问。下面是我的代码。
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/core.hpp>
#include <boost/lambda/bind.hpp>
int main( int argc, char** argv ){
typedef std::vector< std::string > array;
namespace bl = boost::lambda;
size_t ( std::string::* fp )( const std::string&, size_t ) const
= &std::string::find;
std::string to_find( "AA" );
size_t pos = 0;
const char* data [] = { "AAAA","BBBB","","CCAACC","DDDDD" };
array v( data, data +4 );
assert( v.size() == 4 );
std::replace_if(
v.begin()
,v.end()
, bl::bind(
fp
, bl::_1
, bl::constant_ref( to_find )
, bl::var( pos )
) != bl::bind( &std::string::npos, bl::_1 )
, "||"
);
return 0;
}
如果我更改比较,
!= bl::bind( &std::string::npos, bl::_1 )
to
!= std::string::npos
它会构建得很好,但我不确定表达式是否格式良好。有时我发现,由于 lambda 中的惰性求值,我没有得到预期的结果(不是在本例中,而是在之前使用 lambda 的测试中),因为调用可能会延迟。
您通常知道访问 boost lambda 中的静态成员的正确方法是什么吗?
谢谢大家
AFG
I am trying to write some simple predicate using boost::lambda
and I am getting tons of errors.
I checked the documentation and I have some doubt on accessing the static variable std::string::npos
in a lambda expression. Below my code.
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/core.hpp>
#include <boost/lambda/bind.hpp>
int main( int argc, char** argv ){
typedef std::vector< std::string > array;
namespace bl = boost::lambda;
size_t ( std::string::* fp )( const std::string&, size_t ) const
= &std::string::find;
std::string to_find( "AA" );
size_t pos = 0;
const char* data [] = { "AAAA","BBBB","","CCAACC","DDDDD" };
array v( data, data +4 );
assert( v.size() == 4 );
std::replace_if(
v.begin()
,v.end()
, bl::bind(
fp
, bl::_1
, bl::constant_ref( to_find )
, bl::var( pos )
) != bl::bind( &std::string::npos, bl::_1 )
, "||"
);
return 0;
}
If I change the comparison
!= bl::bind( &std::string::npos, bl::_1 )
to
!= std::string::npos
it builds fine, but I am not sure if the expression is well formed. Sometimes I found that, because of lazy evaluation in lambda, I didn't get the expected result ( not in this case but in previous test with lambda ) because the call might be delayed.
Do you know in general what would be the right way to access a static member in boost lambda?
I thank you all
AFG
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用以下方法之一即可访问静态变量,
具体取决于输入参数签名
boost::constant_ref
。Accessing a static variable can be done using simply one of the following
Depending on the input parameter signature also
boost::constant_ref
.std::string::npos
是一个静态成员。它不会根据您使用的字符串实例而变化。您对使用!= std::string::npos
的更改是正确的。使用
boost::constant
或boost::var
允许您延迟静态成员值的计算。如果没有任何修饰符,它的值将在replace_if
参数被求值时求值一次(与v.begin()
和v.end 相同的时间) ()
)。如果您需要将计算延迟到执行绑定表达式的位置(在replace_if
内部),请使用boost::constant
或boost::var< /code>,如果静态成员的值在函数过程中发生变化,这些变化将在绑定表达式内可见。
std::string::npos
is a static member. It doesn't vary based on which string instance you're using. Your change to use!= std::string::npos
is correct.Using
boost::constant
orboost::var
allows you to delay evaluation of the static member's value. Without any modifiers, its value will be evaluated once, at the time thereplace_if
parameters are evaluated (the same time asv.begin()
andv.end()
). If you need to delay evaluation until the point where the bound expression is executed (inside ofreplace_if
), then useboost::constant
orboost::var
, and if the static member's value changes over the course of the function, those changes will be visible inside the bound expression.