jeudi 17 septembre 2020

Program with bound method is built but fails to launch

Consider following code compiled under C++11 with gcc:

#include <cstdio>
#include <functional>
#include <unordered_map>
using namespace std;
using namespace std::placeholders;

class Box
{
    char* box;

public:
    Box(): box(new char[128]) {}
    ~Box()
    {
        delete[] this->box;
    }
    void example(int a, int b)
    {
        printf("%d %d", a, b);
    }
};

Box box;
unordered_map<char, function<void(int, int)>> methods = {
    {'a', bind(&Box::example, box, _1, 8)}
};

int main()
{
    fputs("Main called.", stdout);
    return 0;
}

The program can be built without errors but an attempt to launch it is ignored or results in failure.

On Windows 10, launching the resulted exe file via command prompt just prints a blank line with no explanation, even though program should output Main called..

When running this in VS Code's integrated terminal I get somewhat better error message:

The terminal process "C:\WINDOWS\System32\cmd.exe /d /c {path to exe file} failed to launch (exit code: 3221226356).

When I change the Box definition to one that doesn't use a pointer for the box member it works:

class Box
{
    char box[128];

public:
    void example(int a, int b)
    {
        printf("%d %d", a, b);
    }
};

What gives?

Aucun commentaire:

Enregistrer un commentaire