设置为 std::function 的目标已丢失
The target set to std::function is lost
我为std::function设置的目标丢失了,我不知道为什么我忽略了这个程序中的一些小错误。有人可以帮忙吗
class 测试将 lambda 注册到单例 class,但是当尝试调用回调时,std::function 中设置的目标丢失。
#include <iostream>
#include <functional>
using namespace std;
class callback_store final
{
using callback_func_type = std::function<void()>;
public:
static callback_store instance()
{
static callback_store instance;
return instance;
}
void register_callback(callback_func_type func)
{
std::cout << "register_callback() <<<" << std::endl;
m_func = func;
// I am able to call the targets from here.
std::cout << "register_callback() func() " << std::endl;
func();
std::cout << "register_callback() m_func() " << std::endl;
m_func();
// some stats
std::cout << "register_callback() :: " << func.target_type().name() << std::endl;
}
void invoke_callback()
{
std::cout << "invoke_callback() <<< " << std::endl;
if (!m_func)
{
// This block is hit! Why
std::cout << "invoke_callback() :: m_func empty" << std::endl;
return;
}
return m_func();
}
private:
callback_func_type m_func;
};
class Test final
{
public:
Test()
{
callback_store::instance().register_callback([this](){do_test();});
}
~Test()
{
std::cout << "destructor" << std::endl;
}
private:
void do_test()
{
std::cout << "do_test() invoked !!" << std::endl;
}
};
int main()
{
cout << "Hello World" << endl;
Test t;
callback_store::instance().invoke_callback();
return 0;
}
输出:
sh-4.3$ g++ -std=c++11 -o main *.cpp
sh-4.3$ main
Hello World
register_callback() <<<
register_callback() func()
do_test() invoked !!
register_callback() m_func()
do_test() invoked !!
register_callback() :: ZN4TestC4EvEUlvE_
invoke_callback() <<<
invoke_callback() :: m_func empty
destructor
instance()成员函数应该returncallback_store&
我认为问题在于您实现单例的方式 class。
public:
static callback_store instance()
{
static callback_store instance;
return instance;
}
您返回的是静态对象的副本,您应该使用
public:
static callback_store& instance()
{
static callback_store instance;
return instance;
}
你的代码输出将被(测试):
Hello World
register_callback() <<<
register_callback() func()
do_test() invoked !!
register_callback() m_func()
do_test() invoked !!
register_callback() :: ZN4TestC4EvEUlvE_
invoke_callback() <<<
do_test() invoked !!
destructor
我为std::function设置的目标丢失了,我不知道为什么我忽略了这个程序中的一些小错误。有人可以帮忙吗
class 测试将 lambda 注册到单例 class,但是当尝试调用回调时,std::function 中设置的目标丢失。
#include <iostream>
#include <functional>
using namespace std;
class callback_store final
{
using callback_func_type = std::function<void()>;
public:
static callback_store instance()
{
static callback_store instance;
return instance;
}
void register_callback(callback_func_type func)
{
std::cout << "register_callback() <<<" << std::endl;
m_func = func;
// I am able to call the targets from here.
std::cout << "register_callback() func() " << std::endl;
func();
std::cout << "register_callback() m_func() " << std::endl;
m_func();
// some stats
std::cout << "register_callback() :: " << func.target_type().name() << std::endl;
}
void invoke_callback()
{
std::cout << "invoke_callback() <<< " << std::endl;
if (!m_func)
{
// This block is hit! Why
std::cout << "invoke_callback() :: m_func empty" << std::endl;
return;
}
return m_func();
}
private:
callback_func_type m_func;
};
class Test final
{
public:
Test()
{
callback_store::instance().register_callback([this](){do_test();});
}
~Test()
{
std::cout << "destructor" << std::endl;
}
private:
void do_test()
{
std::cout << "do_test() invoked !!" << std::endl;
}
};
int main()
{
cout << "Hello World" << endl;
Test t;
callback_store::instance().invoke_callback();
return 0;
}
输出:
sh-4.3$ g++ -std=c++11 -o main *.cpp
sh-4.3$ main
Hello World
register_callback() <<<
register_callback() func()
do_test() invoked !!
register_callback() m_func()
do_test() invoked !!
register_callback() :: ZN4TestC4EvEUlvE_
invoke_callback() <<<
invoke_callback() :: m_func empty
destructor
instance()成员函数应该returncallback_store&
我认为问题在于您实现单例的方式 class。
public:
static callback_store instance()
{
static callback_store instance;
return instance;
}
您返回的是静态对象的副本,您应该使用
public:
static callback_store& instance()
{
static callback_store instance;
return instance;
}
你的代码输出将被(测试):
Hello World
register_callback() <<<
register_callback() func()
do_test() invoked !!
register_callback() m_func()
do_test() invoked !!
register_callback() :: ZN4TestC4EvEUlvE_
invoke_callback() <<<
do_test() invoked !!
destructor