0

So, I have a class that takes an integer, a vector of integers, and an array of lambda functions for its constructor, like so:

class Cell {
private:
    short m_State;
    std::function< bool(const char) > m_func;

public:
    Cell(short state, std::function< bool(const char) > func) { m_State = state; m_func = func; };
    Cell() { m_State = 0; m_func = [] (char) -> bool { return false; }; };
    bool operator() (const char input) { return m_func(input); };
    short State() { return m_State; };
};


class Row {
private:
    Cell * m_Cells;
    short m_States;

public:
    Row( short num_states, std::vector<short> states, std::function< bool(char) > * funcs ) {
        m_States = num_states;
        m_Cells = new Cell[num_states];
        for (short i = 0; i < m_States; i++) {
            m_Cells[i] = Cell( states[i], funcs[i] );
        }
    };

    ~Row() {
        delete[] m_Cells;
        m_Cells = NULL;
        m_States = 0;
    };

    short operator[] (const char input) const {
        for (short i = 0; i < m_States; i++)
            if (m_Cells[i](input))
                return m_Cells[i].State();
        return -1;
    };
};

Now, suppose I have some lambdas:

auto kIS_BETWEEN = [](char s, char e) { return [s, e](char x) -> bool { return (x >= s && x <= e); }; };
auto kIS_NOT_0_DIGIT_FUNC = kIS_BETWEEN('1', '9')

Further suppose that I have a preprocessor definition like so:

#define FUNC_TYPE(x) const std::function< bool(const char) >[x]

Now, when I try and define a Row variable:

Row x = Row( 1, {1}, (FUNC_TYPE(1)){kIS_NOT_0_DIGIT_FUNC} );

I get a "taking address of temporary array" error. What am I doing wrong and how can I fix it?

Woody1193
  • 7,252
  • 5
  • 40
  • 90

1 Answers1

0

The key was to change my Row class such that:

class Row {
private:
    std::vector<Cell> m_Cells;
    short m_States;

public:
    Row( short num_states, std::vector<short> states, std::vector< std::function< bool(char) > > funcs ) {
        m_States = num_states;
        for (short i = 0; i < m_States; i++) {
            m_Cells.push_back(const Cell( states[i], funcs[i] ));
        }
    };

    short operator[] (const char input) const {
        for (short i = 0; i < m_States; i++)
            if (m_Cells[i](input))
                return m_Cells[i].State();
        return -1;
    };
};
Woody1193
  • 7,252
  • 5
  • 40
  • 90