smac89
smac89

Reputation: 43078

c++11 vector of threads as class member

Can someone explain why I can't write a vector of threads like this:

//This is declared in a namespace
const uint MAXTHREADSAMOUNT = std::thread::hardware_concurrency();
//...

//declared in the same namespace
class AI {
    static vector<std::thread> Helpers(MAXTHREADSAMOUNT);
};

instead the compiler is forcing me to use this wierd looking method:

class AI {
    static vector<std::thread> Helpers(std::thread);
};

The error message I get when compiling the first one is:

error: 'MAXTHREADSAMOUNT' is not a type

It has nothing to do with the vector being static, but I notice that the first method works if the vector is not declared inside a class or struct object.

So my question is why does vector need the type being stored to be passed explicitly via the constructor rather than using the type already declared in the template?

Upvotes: 1

Views: 381

Answers (1)

aaronman
aaronman

Reputation: 18750

You can't initialize a static data member inline, the second version is wrong too, it's actually a function declaration that returns a vector of threads and takes a thread. Just initialize it outside the class like your supposed to.

vector<std::thread> AI::Helpers(MAXTHREADSAMOUNT);

Upvotes: 4

Related Questions