How access class functions with <future> library? - c++

I'm trying to make a program with #include <future> library.
When I try to access a header function, I get an error.
no instance of overloaded function "async" matches the argument list -- argument types are: (std::launch, int ()) [line 16, 14]
a pointer to a bound function may only be used to call the function
[line 16, 37]
main.cpp:
#include "TEST.h"
#include <future>
#include <iostream>
using namespace std;
using namespace Class;
FNH f;
int main(){
auto fn = async(launch::async, f.selam);
}
TEST.h:
#pragma once
#include <iostream>
using namespace std;
namespace Class{
class FNH{
public:
int selam(){
cout << "selam";
return 1;
}
};
}
I'm a beginner at coding so I really don't know how to fix it or if it's possible.

You can pass a member function pointer and the instance of the class it will be called on:
#include <future>
#include <iostream>
namespace Class {
class FNH {
public:
int selam(){
std::cout << "selam";
return 1;
}
int selam_2(int a, int b){
std::cout << "selam "<< a << " " << b;
return 1;
}
};
}
int main(){
Class::FNH f;
// Member Function Pointer
auto fn = std::async(std::launch::async, &Class::FNH::selam, f);
// Member Function Pointer with arguments
auto fn2 = std::async(std::launch::async, &Class::FNH::selam_2, f, 1, 2);
}

Put the method call in a lambda
#include <future>
#include <iostream>
namespace Class{
class FNH {
public:
int selam(){
std::cout << "selam";
return 1;
}
};
}
int main(){
Class::FNH f;
auto fn = std::async(std::launch::async, [&f]{ return f.selam(); });
}

Related

Create a constructor for a shared_ptr<MyClass>

#include <iostream>
#include <vector>
class TestX {
public:
int i;
TestX(int inp1) : i(inp1){}
};
using Test = std::shared_ptr<TestX>;
int main()
{
Test a(4);
std::cout << a->i << std::endl;
}
I wanted to hide away that I am using a shared pointer, and make it look like I have just a regular class. The reason is that it is essential that my objects are never copied, but I still want the users to be able to create a vector with {obj1, obj2}. Is there a way to initialize a Test object as if there was a constructor, or do I have to use make_shared to initialize it?
You can use a class to wrap a std::shared_ptr, as follows
#include <iostream>
#include <vector>
#include <memory>
struct TestX {
int i;
TestX(int inp1) : i(inp1){}
TestX(TestX const &) = delete;
};
struct Test {
std::shared_ptr<TestX>test;
Test(int inp1) : test{std::make_shared<TestX>(inp1)}{}
int& get_i (){
return test -> i;
}
};
int main()
{
Test a(4);
Test b(1);
auto v = std::vector{a, b};
std::cout << a.get_i() << std::endl;
}
you can also derive from shared_ptr<TestX>
#include <iostream>
#include <vector>
#include <memory>
class TestX {
public:
int i;
TestX(int inp1) : i(inp1){}
};
struct Test : std::shared_ptr<TestX>{
Test( int x) : std::shared_ptr<TestX>{std::make_shared<TestX>(x)}{}
};
int main()
{
Test a(4);
std::cout << a->i << std::endl;
Test b(1);
auto v = std::vector{a, b};
}

using boost::function with instance methods

I am trying to use boost::function with instance methods using the following example
class someclass
{
public:
int DoIt(float f, std::string s1)
{
return 0;
}
int test(boost::function<int(float, std::string)> funct)
{
//Funct should be pointing to DoIt method here
funct(12,"SomeStringToPass");
}
void caller()
{
test(DoIt); //Error : 'someclass::DoIt': function call missing argument list; use '&someclass::DoIt' to create a pointer to member
}
};
Any suggestion on how I could resolve this issue ?
You should use boost::bind:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>
using namespace std;
class someclass
{
public:
int DoIt(float f, std::string s1)
{
return 0;
}
int test(boost::function<int(float, std::string)> funct)
{
return funct(5.0, "hello");
}
void caller()
{
cout << test(boost::bind(&someclass::DoIt, this, _1, _2)) << endl;
}
};
int main() {
someclass s;
s.caller();
}

How to register member functions in map c++

I am trying to create a map to hold functions that can be registered and fired. I cannot seem to get the correct bind / function / pointer syntax in order to get this compiling properly.
Here is what I have: I have tried both boost::bind and boost:
#include <cstdlib>
#include <iostream>
#include <boost/bind/bind.hpp>
#include <boost/function.hpp>
#include <map>
using namespace std;
typedef const std::string& listenArg;
typedef void (*Actions)(listenArg str);
std::multimap<int, Actions> functions;
// fire in the hole!
void fire(int methods, listenArg arg0) {
std::multimap<int, Actions>::iterator function = functions.find(methods);
typedef std::pair<int, Actions> pear;
for (function = functions.begin(); function != functions.end(); ++function) {
(*(function->second))(arg0);
}
}
void listen1(listenArg arg0) {
std::cout << "listen1 called with " << arg0 << std::endl;
}
class RegisteringClass {
public:
RegisteringClass();
virtual ~RegisteringClass();
void callMeBaby(listenArg str) {
std::cout << "baby, i was called with " << str << std::endl;
}
};
int main(int argc, char** argv) {
const int key = 111;
functions.insert(make_pair<int, Actions>(key, listen1));
fire(key, "test");
// make a registeringClass
RegisteringClass reg;
// register call me baby
boost::function<void (listenArg) >
fx(boost::bind(&RegisteringClass::callMeBaby, reg, _1));
//std::bind(&RegisteringClass::callMeBaby, reg, _1);
functions.insert(
make_pair<int, Actions> (key, fx));
// fire
fire(key, "test2");
return 0;
}
Thanks for any help!
typedef boost::function < void (listenArg) > Actions;
Should be used instead of function pointer.
The problem is that you're telling the compiler that Actions is a non-member function pointer, and then you try to put a boost::function into a variable of that type. They're two totally unrelated types and such an assignment can't happen. You need to make your Actions typedef be a boost::function<void (listenArg)> instead.
you can use boost::function template
#include <cstdlib>
#include <iostream>
#include <boost/bind/bind.hpp>
#include <boost/function.hpp>
#include <map>
using namespace std;
typedef const std::string& listenArg;
typedef boost::function < void (listenArg) > Actions;
std::multimap<int, Actions> functions;

Passing vector by reference in boost::bind

Why is the output of this 0?
http://ideone.com/S7hgv
#include <boost/bind.hpp>
#include <vector>
#include <iostream>
using namespace std;
void f2(vector<int> &h)
{
h.clear();
h.push_back(0);
}
void f1(vector<int> &h)
{
boost::bind(f2, boost::ref(h));
}
int main()
{
vector<int> h;
f1(h);
cout << h.size() << "\n";
}
I need it to be 1, and for some reason h is not modified.
boost/std::bind() only constructs the function object. You still have to call it, in order for any code inside to execute.
To get the output of 1, replace the line
boost::bind(f2, boost::ref(h));
with
boost::bind(f2, boost::ref(h))();

how to print a list<class*> ls?

What am I doing wrong at the last two lines in my code below? I receive an error:
request for member ‘name’ in ‘t.std::_List_iterator<_Tp>::operator* [with _Tp = a*]()’, which is of non-class type ‘a*’
#include <dlfcn.h>
#include <iostream>
#include "/home/developer/Desktop/MsgSphSDK1/test1_sdk/libsdk_MS.hpp"
#include <list>
#include <typeinfo>
#include <string>
using namespace std;
class a
{
public:
string name;
int age;
};
int main()
{
a* l = new a();
l->name = "me";
l->age = 1;
list<a*> ls;
list<a*>::iterator t;
for (t = ls.begin(); t != ls.end(); ++t)
cout << (*t).name << endl;
}
You should write
cout<<(*t)->name<<endl
t is an iterator, (*t) gives you a* (the pointer to the object of class a). So to get access to the member of the object you should use ->, not ..