Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::bind() doesn't bind arguments

Tags:

c++

I'm trying to create a queue of methods which are bound to objects using std::bind(). The parameter (object of another class) of the method is bound when std::bind() is called. For some reason I cannot get it to work.

parser.h

#ifndef PARSER_H
#define PARSER_H
class File_Descriptor;
class Parser{
public:
    void parse(File_Descriptor *file_descr);
};
#endif

parser.cpp

#include <iostream>
#include "parser.h"
#include "file_descriptor.h"
using namespace std;
void Parser::parse(File_Descriptor *file_descr){
    cout << file_descr->id << endl;
}

file_descriptor.h

#ifndef FILE_DESCRIPTOR_H
#define FILE_DESCRIPTOR_H
#include "parser.h"
class File_Descriptor{
public:
    File_Descriptor(const int &id);
    Parser parser;
    int id;
};
#endif

file_descriptor.cpp

#include "file_descriptor.h"
File_Descriptor::File_Descriptor(const int &id):id(id){}

main.cpp

#include <functional>
#include <queue>
#include "file_descriptor.h"
#include "parser.h"
using namespace std;
int main(){
    File_Descriptor file_descr(123);
    function<void(File_Descriptor*)> task = bind(&Parser::parse, &file_descr.parser, &file_descr);
    
    //task(); file_descr(123) is not bound to &Parser::parse(File_Descriptor *file_descr)
    
    queue<function<void(File_Descriptor*)>> task_queue;
    task_queue.push(task);
    function<void(File_Descriptor*)> exec_task = task_queue.pop();
    exec_task();
    return 0;
}

error msg

main.cpp: In function ‘int main()’:
main.cpp:17:64: error: conversion from ‘void’ to non-scalar type   
‘std::function<void(File_Descriptor*)>’ requested
   17 |     function<void(File_Descriptor*)> exec_task = task_queue.pop();
      |                                                  ~~~~~~~~~~~~~~^~
main.cpp:18:15: error: no match for call to ‘(std::function<void(File_Descriptor*)>) ()’
   18 |     exec_task();
      |               ^
In file included from /usr/include/c++/9/functional:59,
             from main.cpp:1:
/usr/include/c++/9/bits/std_function.h:683:5: note: candidate: ‘_Res    std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = void; _ArgTypes = {File_Descriptor*}]’
  683 |     function<_Res(_ArgTypes...)>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/std_function.h:683:5: note:   candidate expects 1 argument, 0 provided
like image 497
Mikey Avatar asked Jul 04 '26 05:07

Mikey


1 Answers

If you have a function with two arguments, and you bind both of them, how many are left? Not one, zero. So your std::functions are wrong and should be std::function<void()>. Nothing is wrong with std::bind. Also std::queue<T>::pop returns void, not the popped element. You have to use front, then pop.

int main() {
    File_Descriptor file_descr(123);
    std::function<void()> task = std::bind(&Parser::parse, &file_descr.parser, &file_descr);
    task();
    
    std::queue<std::function<void()>> task_queue;
    task_queue.push(task);
    std::function<void()> exec_task = std::move(task_queue.front());
    task_queue.pop();
    exec_task();
}
like image 103
HTNW Avatar answered Jul 05 '26 18:07

HTNW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!