environment: vs2013 rc5 / vs2017;
project: win32 console application;
representation:compile and run a little while,then interrupt and watch variable "task_";
if "add_task(&Test::print, &t, str, 10)" in man, the "task_" is correct value: correct
but if "add_task(&Test::print, &t, str, 10)" in mytest, the "task_" is wrong value; and if replace std::cin.get() with while(1){}, it turns right:
[wrong][2]
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <functional>
using task_t = std::function<void()>;
class Test
{
public:
void print(const std::string& str, int i)
{
std::cout << "Test: " << str << ", i: " << i << std::endl;
}
};
template<typename Function, typename Self, typename... Args>
void add_task(const Function& func, Self* self, Args... args)
{
task_t task = [&func, &self, args...]{ return (*self.*func)(args...); };
task_ = task;
}
Test t;
std::string str = "Hello world";
task_t task_ = nullptr;
void mytest()
{
add_task(&Test::print, &t, str, 10);
}
int main()
{
mytest();
std::cin.get();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire