我是新手来提升精神,我有以下问题:
#include <string>
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/bind.hpp>
using namespace boost::spirit;
using namespace std;
struct MyGrammar
: qi::grammar<string::const_iterator, string(), ascii::space_type> {
MyGrammar();
void myFun(const string& s);
private:
qi::rule<string::const_iterator, string(), ascii::space_type> myRule;
};
using namespace boost::spirit;
using namespace std;
MyGrammar::MyGrammar() : MyGrammar::base_type(myRule) {
using qi::_1;
myRule = int_ [boost::bind(&MyGrammar::myFun, this, _1)]; // fails
myRule = int_ [_val = _1]; // fine
}
void MyGrammar::myFun(const string& s){
cout << "read: " << s << endl; …Run Code Online (Sandbox Code Playgroud) 我有一个std ::类列表,并希望删除标记为删除的条目.我正在使用std :: remove_if和erase.
class MyClass
{
bool isDone(MyData& myData)
{
return myData.isDone();
}
void removeIfDone(std::list<MyData>& myList)
{
std::list<MyData>::iterator it =
remove_if(myList.begin(), myList.end(),
boost::bind(&MyClass::isDone, this, _1));
myList.erase(it, myList.end());
}
};
Run Code Online (Sandbox Code Playgroud)
我正在一个小型处理器上运行,内存分配和释放非常昂贵.此删除在我的应用程序中调用new并删除数千次.
我以前在使用boost::ref非平凡变量作为绑定参数时使用过,但在这种情况下,我认为它可能是仿函数本身的创建和破坏,或者是造成问题的复制.
我想做点什么
boost::bind(&MyClass::isDone, boost::ref(this), boost::ref(_1));
Run Code Online (Sandbox Code Playgroud)
我找不到有关正在创建和销毁的内容的文档.所以我的简单问题是如何提高效率呢?
我正试图从boost :: spirit规则定义的动作中引用(尚未)未知实例的成员,因此在伪代码中,
而不是double_ [ref(rN)= _1]我正在寻找类似X**ppx的东西; double_ [ref(&X :: rN,ppx)= _1]
它的解决方法可能是一个简单的"语义操作",其中一个参数可以知道实例并且可以写入它,就像
qi::rule<Iterator, Skipper> start;
my_grammar(DataContext*& dataContext) : my_grammar::base_type(start) , execContext(execContext) {
start = qi::double_[ boost::bind(&my_grammar::newValueForXY, dataContext, ::_1) ];
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否有可能直接"绑定"到成员变量,就像可以通过使用"phoenix :: ref(...)= value"绑定到"本地"变量一样.
我尝试了以下语法:
start = qi::int_[ boost::bind<int&>(&DataContext::newValueForXY, boost::ref(dataContext))() = ::_1] ];
Run Code Online (Sandbox Code Playgroud)
但是VS2010SP1和错误消息失败了
错误C2440:'=':'boost :: arg'无法转换为...
我有一个像这样的对象类型:
struct T
{
int x;
bool y;
};
Run Code Online (Sandbox Code Playgroud)
和他们的容器像这样:
std::vector<T> v;
Run Code Online (Sandbox Code Playgroud)
和强烈的愿望来决定- 在单个语句 -任何元素是否v有y == true.这可能涉及到std::find_if.
我的理解是,std::bind并且boost::bind适用于成员函数,不能应用于成员数据.
因为我不喜欢他们,我希望避免:
因为我的环境是C++ 03,所以以下内容不可用:
我的应用程序有一个类似于以下代码的部分
void SomeClass::OtherMethod(std::vector<std::string>& g)
{
g.pushback("Something");
}
void SomeClass::SomeMethod()
{
std::vector<std::string> v;
boost::thread t(boost::bind(&SomeClass::OtherMethod,this,v)
t.join();
std::cout << v[0]; //Why is this empty when the vector created on stack
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么当在堆栈上创建向量时向量v为空,并且当它在堆上创建时它可以工作.我期待上面的代码能够工作,因为即使在堆栈上创建了向量,向量仍然在范围内.
我想定义一个带有2个参数的函数
double func(double t, double x);
Run Code Online (Sandbox Code Playgroud)
从外部文本文件中读取实际实现的位置.
例如,在文本文件中指定
function = x*t;
Run Code Online (Sandbox Code Playgroud)
功能应该实现的乘法x和t,以便它可以在稍后阶段被调用.我正在尝试使用boost :: spirit来解析函数.但我不知道如何实现它.
下面,我创建了一个实现乘法的简单函数.我将它绑定到boost函数,我可以使用它.我还创建了一个简单的语法,它解析了两个双精度数之间的乘法.
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include "boost/function.hpp"
#include "boost/bind.hpp"
#include <boost/spirit/include/qi_symbols.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace ascii=boost::spirit::ascii;
using boost::spirit::ascii::space;
using boost::spirit::qi::symbols;
template< typename Iterator >
struct MyGrammar : public virtual qi::grammar< Iterator, ascii::space_type >
{
MyGrammar() : MyGrammar::base_type(expression)
{
using qi::double_;
//The expression should take x and t as symbolic expressions
expression = (double_ >> '*' …Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C++应用程序,它执行以下操作:
template< typename Fcn >
inline void Bar( Fcn fcn ) // line 84
{
fcn();
};
template< typename Fcn >
inline void Foo( Fcn fcn )
{
// this works fine
Bar( fcn );
// this fails to compile
boost::bind( Bar, fcn )();
};
int main()
{
SYSTEM_POWER_STATUS_EX status = { 0 };
Foo( boost::bind( ::GetSystemPowerStatusEx, &status, true ) ); // line 160
return 0;
}
Run Code Online (Sandbox Code Playgroud)
*对GetSystemPowerStatusEx()的调用仅用于演示.在那里插入您最喜欢的电话,行为是一样的.
当我编译这个时,我得到84个错误.除非被问到,否则我不会发布所有内容,但他们从这开始:
1>.\MyApp.cpp(99) : error C2896: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* …Run Code Online (Sandbox Code Playgroud) 我想使用std :: for_each将一系列字符串添加到组合框中.对象是类型的Category,我需要调用GetName它们.我怎样才能实现这一目标boost::bind?
const std::vector<Category> &categories = /**/;
std::for_each(categories.begin(), categories.end(), boost::bind(&CComboBox::AddString, &comboBox, _1);
Run Code Online (Sandbox Code Playgroud)
当前代码在尝试调用时失败CComboBox::AddString(category).这显然是错的.如何CComboBox::AddString(category.GetName())使用当前语法调用?
如何使用任意非静态类方法调用 AfxBeginThread?也许我可以用boost bind做点什么?以下是 Microsoft 的预期用法(并且是调用非静态方法的示例,但它是硬编码的方法):
UINT MyThreadProc( LPVOID pParam )
{
CMyObject* pObject = (CMyObject*)pParam;
if (pObject == NULL ||
!pObject->IsKindOf(RUNTIME_CLASS(CMyObject)))
return 1; // if pObject is not valid
// do something with 'pObject'
return 0; // thread completed successfully
}
// inside a different function in the program
...
pNewObject = new CMyObject;
AfxBeginThread(MyThreadProc, pNewObject);
Run Code Online (Sandbox Code Playgroud) boost::bind 无法绑定通过前向声明声明的参数.
有谁能解释为什么?这是一个助推错误吗?
示例代码:
#include "boost/function.hpp"
#include "boost/bind.hpp"
#include <vector>
#include <iostream>
class B;
class A
{
public:
A() {}
void func1(int i) { std::cout << __PRETTY_FUNCTION__ << "(" << i << ")\n"; }
void func2(const std::string& s) { std::cout << __PRETTY_FUNCTION__ << "(" << s << ")\n"; }
void func3(const B& b) { std::cout << __PRETTY_FUNCTION__ << "(" << "unknown" << ")\n"; }
static void dispatch( std::vector<A>& vect, boost::function<void(A &)> const& func)
{
for ( std::vector<A>::iterator iter = vect.begin(); …Run Code Online (Sandbox Code Playgroud) boost-bind ×10
c++ ×10
boost ×8
boost-spirit ×3
boost-thread ×1
c++03 ×1
ref ×1
reference ×1
remove-if ×1
stl ×1
windows ×1