I have a class A that has vectors:
std::vector < std::shared_ptr<B> > bvec;
std::vector<std::future<void> > futures;
Where B is another class.
I have a method:
void A::Method() const {
for (auto&& b : bvec) {
futures.push_back(std::async(std::launch::async, fun));
}
}
Where:
void fun(std::shared_ptr<B> b) {
b->bMethod();
}
Since there is that "const" after "A::Method()", I cannot modify future inside that method. Creating a vector locally/globally doesn't help. Is there another way to optimize the for loop inside of the A::Method() without removing "const"?
CodePudding user response:
Remove the const. There is no reason why that function should be const. Lying in your interface is never a good idea.
CodePudding user response:
try setting the future mutable:
mutable std::vector<std::future<void> > futures;
shoud do the trick (that said, may not be a good idea on a design stand point, but ths is hard to tell as we know little of your design...)
