1

This is for my actual project, but I made a minimal example (code is based on an example of Lightness Races in Orbit).

#include <thread>
#include <iostream>

class Foo
{
    Foo(int n = 10)
    {
        size_t a[n];
        constexpr int p = 5;
        std::thread threads[p];
        for (int i = 0; i < p; ++i)
            threads[i] = std::thread(std::bind(&Foo::bar, this, a, n));

        for (auto& th : threads) th.join();
    }

    void bar(size_t* a, int n) {}
};

int main() {std::cout << "ok\n";}

Live On Coliru

The error comes by the fact that I am using an array that has n as its size. However, it will be very difficult for me -in the real project- to change that, since many lines of code are based on that.

9
  • 1
    How about using a vector? Commented Jan 14, 2015 at 21:05
  • 2
    Have you tried passing &a[0]? Commented Jan 14, 2015 at 21:05
  • 4
    Hopefully, you learned your lesson about using variable-length arrays in C++... Commented Jan 14, 2015 at 21:06
  • How about doing explicit dynamic allocation with the new operator instead of using the variable-length array... those things always have acted funny for me. Commented Jan 14, 2015 at 21:07
  • 3
    @RubixRechvin I think you mean "how about using vector" :P Commented Jan 14, 2015 at 21:07

1 Answer 1

7

Use a vector

Or solve the problem by decaying the array into a pointer before type deduction takes place:

std::bind(&Foo::bar, this, +a, n)

The problem is, bind is deducing an array reference and then tries to copy it /by value/. Array copy is not specified by the language.

Sign up to request clarification or add additional context in comments.

7 Comments

Nice trick! BTW "use a vector" would have different semantics, because the thread would have its own copy. Worth pointing that out.
Thank you sehe, I am trying to make the concepts you and another user introduced to me in another post if you remember. :) However, what about &a[0]? It works too. Which one should I prefer? I also do not understand what we did in your answer, +a is something new to me.
@G.Samaras The unary operator + applied to the array makes it decay to a pointer. See this example. It is a nice trick, but I would probably do something more explicit (such as &a[0]).
@G.Samaras Applying unary + causes the array to decay into a pointer to the first element. If I had to use a C-style array, I'd use &a[0] because the former would confuse a lot of people. Also, I don't see why you need bind at all to solve your problem, passing those argument to the thread constructor should be sufficient.
You are right Praetorian, bind is redundant. Thanks, I will use &a[0] for readability. Thanks for the example juanchopanza, really straightforward.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.