0
#include <thread>

void f(int*& ptr)
{
    ptr = new int[4];
    ptr[0] = 0;
    ptr[1] = 1;
    ptr[2] = 2;
    ptr[3] = 3;
}

int main()
{
    int* ptr;

    std::thread thread{ f, ptr };
    thread.join();

    delete[] ptr;
    return 0;
}

I don't understand what I'm missing. I tried different combinations of reference or non-reference, checked whether I have the same format as in the documentation. Nothing. I still get this error:

Error C2672: 'std::invoke': no matching overloaded function found

1 Answers1

1

The problem is that your function f takes a reference to a pointer. However, threads can't store reference-arguments for later invocation, so you must use a pointer to a pointer instead.

#include <thread>

void f(int** ptr)
{
    *ptr = new int[4];
    *ptr[0] = 0;
    *ptr[1] = 1;
    *ptr[2] = 2;
    *ptr[3] = 3;
}

int main()
{
    int* ptr;

    std::thread thread{f, &ptr};
    thread.join();

    delete[] ptr;
    return 0;
}

Note: You can initialize the array in a much prettier way using:

*ptr = new int[] {0, 1, 2, 3};
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96