我正在使用TR1 std::function来实现一个简单的回调机制.如果我不想nullptr回调,我会注册为回调处理程序.这编译并正常工作:
void Foo::MessageHandlingEnabled( bool enable ){
if( enable )
m_Bar.RegisterMessageHandler( std::bind(&Foo::BarMessageHandler, this, std::placeholders::_1) );
else
m_Bar.RegisterMessageHandler( nullptr );
}
Run Code Online (Sandbox Code Playgroud)
如果我使用三元运算符重写它...
void Foo::MessageHandlingEnabled( bool enable ){
m_Bar.RegisterMessageHandler( enable?
std::bind(&Foo::BarMessageHandler, this, std::placeholders::_1) :
nullptr );
}
Run Code Online (Sandbox Code Playgroud)
... VC++的编译器说:
错误C2446:':':没有从'nullptr'转换为'std :: tr1 :: _ Bind <_Result_type,_Ret,_BindN>'1> 1>
[1> _Result_type = void,1> _Ret = void,1>
_BindN = std :: tr1 :: _ Bind2,Foo*,std :: tr1 :: _ Ph <1 >> 1>] 1>没有构造函数可以采用源类型,或者构造函数重载解析是模糊的
这是编译器的限制,还是我做了一些愚蠢的事情?我知道在这种特殊情况下,使用三元运算符可能无法获得任何好处,但我只是好奇.
我想要一个映射将字符串键映射到a std::function<T()>或std::function<T(int)>(但不是两个都给定键)的映射.我得到一个编译错误,似乎没有模板std::function<T(...)>.我希望能够使用lambdas作为值.
首先,以这种方式存储功能是否安全?其次,如果是这样,语法是什么?(当从地图中检索到函数时,函数是一元函数还是无效函数.)如果不是上述函数,那么什么是合理的替代方案?指针杀死了lambda的想法,对...
C++ 11没问题.
让我展示一下我的意思.
#include <functional>
#include <iostream>
class MyClass
{
private:
int number;
public:
MyClass()
{
number = 0;
}
void printNumber()
{
std::cout << "number is " << number << std::endl;
number++;
}
};
int main()
{
std::shared_ptr<MyClass> obj = std::make_shared<MyClass>();
auto function = std::bind(&MyClass::printNumber, obj);
function();
// This deallocates the smart pointer.
obj.reset();
// This shouldn't work, since the object does not exist anymore.
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这输出:
number is 0
number is 1
Run Code Online (Sandbox Code Playgroud)
如果我们像通常那样调用函数,将"function()"替换为"obj-> printNumber()",则输出为:
number is 0 …Run Code Online (Sandbox Code Playgroud) 我想在一个类中存储一个函数,只需在一个成员函数中调用该函数.我知道这可以使用函数指针,但我想用std::function它.
以下是一些不起作用的代码,但应该演示我想要做的事情:
double foo(double a, double b){
return a + b;
}
class Test{
private:
std::function<double(double,double)> foo_ ;
public:
Test(foo);
void setFoo(foo) {foo_ = foo;}
double callFoo(double a, double b){return foo_(a,b);}
};
int main(int argc, char const *argv[]) {
Test bar = Test(foo);
bar.callFoo(2,3);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要将一个参数绑定到类成员函数。像这样的东西:
#include <functional>
#include <iostream>
struct test
{
void func(int a, int b)
{
std::cout << a << " " << b << std::endl;
}
};
int main(int argc, char** argv)
{
typedef void (test::*TFunc)(int);
TFunc func = std::bind(&test::func, 1, std::placeholders::_1);
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下我有编译错误
error: static assertion failed: Wrong number of arguments for pointer-to
-member
Run Code Online (Sandbox Code Playgroud) 我有一个std :: functions的向量
Using Functors = std::vector<std::function<bool(int)>>;
Functors fs;
Run Code Online (Sandbox Code Playgroud)
现在,如果我想向向量添加一个函数,我会:
fs.emplace_back(
[](int v)->bool { return v > 1; }
)
Run Code Online (Sandbox Code Playgroud)
我想知道什么时候我声明"Functors",我应该指向std :: function而不是raw std :: function吗?这意味着,我会这样声明:
Using Functors = std::vector<
std::function<bool(int)>*
>; // note the last &
Run Code Online (Sandbox Code Playgroud)
因此,当我调用emplace_back时,我只会传入指针而不是复制?或者lambda闭合可以移动?请在这里说明一下:我何时应该使用std :: function的引用?
使用C++ 14编译以下代码,但运行它会导致分段错误.这是由lambda函数捕获引起的(用问号标注)?这样做的正确方法是什么?提前致谢.
#include <functional>
#include <iostream>
#include <memory>
struct Process {
Process(std::function<void()> &processFunc)
: processFunc(processFunc) {}
void doit() {
processFunc(); // causes segmentation fault
}
std::function<void()> &processFunc;
};
struct Foo {
Foo() {
std::function<void()> func = [this](){this->process();}; // ?
p = std::make_unique<Process>(func);
}
void process() {std::cout << "Done.\n";}
void start() {p->doit();}
std::unique_ptr<Process> p;
};
int main()
{
Foo foo;
foo.start();
}
Run Code Online (Sandbox Code Playgroud) 我想动态链接共享库并从中分配一个函数std::function.这是代码:
function.cpp:
#include <array>
#ifdef __cplusplus
extern "C" {
#endif
double function(std::array<double, 1> arg)
{
return arg[0] * 2;
}
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <functional>
#include <iostream>
#include <fstream>
#include <array>
#include <functional>
#ifdef __linux__
#include <dlfcn.h>
#endif
int main()
{
void *handle;
double (*function)(std::array<double, 1>);
char *error;
handle = dlopen("/home/oleg/MyProjects/shared_library_test/libFunction.so", RTLD_LAZY);
if (!handle)
{
fprintf(stderr, "%s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void **) (&function) = dlsym(handle, "function");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%s\n", …Run Code Online (Sandbox Code Playgroud) 我正在尝试弄清楚如何将std函数作为参数传递。这是一个简短的例子:
#include <cmath>
#include <functional>
#include <iostream>
void bar(const double v, std::function<int(int)> foo) {
std::cout << foo(v) << std::endl;
}
int main() {
bar(-23., std::abs);
}
Run Code Online (Sandbox Code Playgroud)
此代码无法编译,并显示消息nomatching function for call tobar,但如果删除前缀,一切正常std。这是怎么回事?