我想知道与一个相同的boost :: function调用相比,单继承虚函数调用的速度有多快.它们的性能几乎相同,还是boost :: function慢?
我知道性能可能因情况而异,但是,作为一般规则,哪个更快,哪个程度如此之大?
谢谢,Guilherme
- 编辑
KennyTM的测试对我来说足够令人信服.对于我自己的目的,boost :: function似乎并不比vcall慢得多.谢谢.
我正在尝试std::find_if通过boost::bind与boost::contains(来自boost/algoritm/string库)一起使用来创建谓词.以下片段显示了我试图完成此任务的两种方式.
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <string>
int main(int argc, char** argv) {
std::string s1("hello mom");
std::string s2("bye mom");
boost::function<bool (std::string, std::string)> f = &boost::contains<std::string, std::string>;
std::cout << s1 << " contains " << "hello, " << std::boolalpha << f(s1, "hello") << std::endl;
std::cout << s2 << " contains " << "hello, " << std::boolalpha << f(s2, "hello") << std::endl;
boost::function<bool (std::string)> contain_hello = boost::bind(boost::contains<std::string, std::string>, _1, std::string("hello"));
std::cout << …Run Code Online (Sandbox Code Playgroud) 我实际上是尝试使用boost :: serialize序列化boost :: function,因为我想在boost :: interprocess :: message_queue中共享它.我只看到一种方法,它是使用非侵入式版本的boost :: serialize.
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive & ar, boost::function<void()> & fct, const unsigned int version)
{
ar & fct.args;
ar & fct.arity;
ar & fct.vtable;
ar & fct.functor;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还需要序列化vtable和functor,我没有尝试过,我不确定它是否正常工作.
那么有没有办法以正确的方式序列化boost :: function?
谢谢.
c++ boost boost-serialization boost-interprocess boost-function
说我有这样的功能:
void someFunction(const ExpensiveObjectToCopy&);
Run Code Online (Sandbox Code Playgroud)
如果我将boost :: function输出,那么该函数将在其闭包中存储自己的克隆副本:
boost::function<void()> f = boost::bind(someFunction, x); // <-- f saves a copy of x
Run Code Online (Sandbox Code Playgroud)
现在,如果我开始传递f,boost :: function复制构造函数每次都会复制该对象,还是每个函数共享相同的闭包?(就像这样)
boost::function<void()> f2 = f;
callSomeFunction(f);
etc.
Run Code Online (Sandbox Code Playgroud) 我有一个KeyCallbacks 的向量:
typedef boost::function<void (const KeyEvent&)> KeyCallback
Run Code Online (Sandbox Code Playgroud)
我用它来存储按下键盘按钮时的所有监听器.我可以添加它们并将事件发送到所有回调for_each,但我不知道如何KeyCallback从我的向量中删除特定的签名.
例如,我想要这样的东西:
void InputManager::UnregisterCallback(KeyCallback callback) {
mKeyCallbacks.erase(std::find(mKeyCallbacks.begin(), mKeyCallbacks.end(), callback));
}
Run Code Online (Sandbox Code Playgroud)
根据boost::function文档(见这里),没有比较函数对象的东西,这可以解释我上面的问题.我被困了吗?这有什么好办法吗?
(我读到boost::signals了回调机制,但它显然很慢,我希望回调可能会在一帧内被解雇几次.)
假设我有一个subscribe()调用的函数,它接受一个回调处理程序,当触发事件时将调用它.
现在,我有另一个版本,叫做subscribe2().一切都是相同的,除了触发时,它需要将其发布到事件队列.它是使用原始实现的subscribe(),带有一个名为helper的函数helper().它所做的只是将原始处理程序和任何其他参数绑定到仿函数中,然后调用postToEventQueue().
现在,我想知道是否有一种消除辅助函数的方法,因此subsribe2(),我可以以某种方式直接打包postToTaskQueue()函数和原始回调处理程序,并将其传递给subscribe().原因是我有很多不同的处理程序类型,并且在整个地方引入辅助函数是很乏味和累人的.毕竟,boost :: bind应该在给定原始函数的情况下返回一个新函数,对吧?我试图用boost :: bind直接生成辅助函数.
一种尝试就是说
subscribe(boost::bind(boost::bind(postToTaskQueue, boost::bind(_1, _2)), cb, _1));
Run Code Online (Sandbox Code Playgroud)
在subscribe2(),但它不起作用.有可能吗?
请参阅下面的详细示例代码.谢谢!
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
typedef boost::function<void(int)> SomeCallback;
typedef boost::function<void()> Task;
void handler(int i){
std::cout << "i=" << i <<std::endl;
}
void subscribe(SomeCallback cb)
{
cb(100); //just invoke the callback for simplicity
}
void postToTaskQueue(Task t)
{
t(); // just invoke the task for simplicity
} …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我正在尝试将类模板的朋友函数填充到boost::function:
#include <boost/function.hpp>
template <int N>
struct Vec{
friend
double dot(Vec, Vec){}
};
template class Vec<2>; //Force multiple instantiations
template class Vec<3>;
int main()
{
boost::function<double (Vec<2>, Vec<2>)> func = dot;
double (*func1)(Vec<2>, Vec<2>) = dot;
}
Run Code Online (Sandbox Code Playgroud)
这两行都main不会编译.对于第一个,GCC抱怨:
error: conversion from '<unresolved overloaded function type>' to non-scalar type 'boost::function<double(Vec<2>, Vec<2>)>' requested
Run Code Online (Sandbox Code Playgroud)
第二行的错误似乎让我更加困惑:
error: no matches converting function 'dot' to type 'double (*)(struct Vec<2>, struct Vec<2>)'
testtmpl.C:6:15: error: candidates are: double dot(Vec<2>, Vec<2>)
testtmpl.C:6:15: error: double dot(Vec<3>, …Run Code Online (Sandbox Code Playgroud) 我知道我可以std::function这样输入:
typedef std::function<void (const std::string&)> TextChangedHandler
Run Code Online (Sandbox Code Playgroud)
是否允许在typedef中指定参数名称,以使其更加自我记录?例如:
typedef std::function<void (const std::string& text)> TextChangedHandler
Run Code Online (Sandbox Code Playgroud)
我可以添加参数名称,它在Visual C++ 2010上编译得很好,但我想确保它是C++ 03/C++ 11标准所允许的.
我想定义一个带有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) 这与前一个问题有关:使用boost :: bind和boost :: function:检索绑定变量类型.
我可以绑定一个这样的函数:
在.h:
class MyClass
{
void foo(int a);
void bar();
void execute(char* param);
int _myint;
}
Run Code Online (Sandbox Code Playgroud)
在.cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
boost::function<void(void)> f = myVector[0];
_myint = atoi(param);
f();
}
Run Code Online (Sandbox Code Playgroud)
但是如何绑定返回值呢?即:
在.h:
class MyClass
{
double foo(int a);
void bar();
void execute(char* param);
int _myint;
double _mydouble;
}
Run Code Online (Sandbox Code Playgroud)
在.cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
//PROBLEM IS HERE: HOW DO I BIND "_mydouble"
myVector.push_back(boost::bind<double>(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* …Run Code Online (Sandbox Code Playgroud) boost-function ×10
c++ ×10
boost ×7
boost-bind ×5
boost-lambda ×1
boost-spirit ×1
c++11 ×1
overloading ×1
performance ×1
std-function ×1
templates ×1