Search code examples
c++std-function

Is it possible for a std::function to return a value?


So for a project I'm currently working on, I'm attempting to create a sort of "Function lookup table", basically a map of strings to functions. The function takes in data, modifies it, and then spits it back out. However, it doesn't seem possible for a std::function to return anything.

What I tried (not knowing much about std::function) was to simply write it as so;

map<string, function<Array(vector<pair<char, string>>)>>

Or more specifically,

function<Array(vector<pair<char, string>>)>

(Array is my own class, but everything else is in std)

If I write it without a return;

function<void(vector<pair<char, string>>)>

It works fine.

The error given by gcc is;

no matching constructor for initialization of 'map<std::__1::string, function<Array (vector<pair<char, std::__1::string> >)> >'

Is there any way to be be able to return a value using std::function, or any similar method, or have I completely misunderstood this?

EDIT: Here's the involved code in the problem Main Class (Shortened, there may be extra includes):

#include <iostream>
#include <map>
#include <array>
#include <fstream>
#include <algorithm>
#include <vector>
#include <functional>

#include "Array.h"

using namespace std;

int main() {
    map<string, function<Array(vector<pair<char, string>>)>> dictionary = 
    {
        { 
            "JMP", 
            [](vector<pair<char, string>> operands) {
                if (operands.size() == 1) {
                    char data[2];

                    switch (operands[0].first) {
                    case 3:
                        data[1] = static_cast<char>(bitset<8>(operands[0].second.substr(1, 2)).to_ulong());

                        break;
                    case 4:
                        data[1] = static_cast<char>(bitset<8>(operands[0].second.substr(2, 8)).to_ulong());

                        break;
            case 5:
                data[1] = static_cast<char>(bitset<8>(operands[0].second.substr(2, 8)).to_ulong());

                        break;
                        default:
                            exit(0);
                    }

                    data[0] = 0b00000001;
                    return Array(data, 2);
                } 
                else {
                    exit(2);
                }
            } 
        },
        { 
            "MOV", 
            [](vector<pair<char, string>> operands) {
                return nullptr;
            }
        }
    };

    return 0;
}

Array Class Header:

#ifndef ARRAY_H_
#define ARRAY_H_

struct Array {
public:
    Array();
    Array(char* data, int size);
    virtual ~Array();

    char* data;
    int size;
};

#endif /* ARRAY_H_ */

Array Class Source:

#include "Array.h"

Array::Array() {
    data = nullptr;

    size = 0;
}

Array::Array(char* data, int size) {
    this -> data = data;
    this -> size = size;
}

Array::~Array() {}

Solution

  • Your problem is that your function returns an object of type Array, not a pointer to it. So returning nullptr will obviously result in a compilation error. Try returning an empty Array instead.

    { 
        "MOV", 
        [](vector<pair<char, string>> operands) {
            return Array();
        }
    }