[英]accessing static members using boost lambda
我正在嘗試使用boost::lambda
編寫一些簡單的謂詞,但我遇到了很多錯誤。
我檢查了文檔,我對在 lambda 表達式中訪問 static 變量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 中訪問 static 成員的正確方法是什么?
我謝謝大家
AFG
可以使用以下方法之一訪問 static 變量
boost::constant( std::string::npos )
boost::var( std::string::npos )
根據輸入參數簽名還boost::constant_ref
。
std::string::npos
是static成員。 它不會因您使用的字符串實例而異。 您對使用:= std::string::npos
的更改是正確的。
使用boost::constant
或boost::var
允許您延遲評估 static 成員的值。 如果沒有任何修飾符,它的值將在評估replace_if
參數時評估一次(與v.begin()
和v.end()
相同)。 如果您需要將評估延遲到執行綁定表達式的點(在replace_if
內部),則使用boost::constant
或boost::var
,並且如果 static 成員的值在 function 的過程中發生變化,這些變化將在綁定表達式中可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.