I'm using C++11 to do some thread program.
Now I get such a situation:
I have a std::set<std::future<std::string>> results
to store some results of threads, of course all of these threads will return a string.
However, when I try to get the strings, I get an error as below:
passing xxx as 'this' argument of xxx discards qualifiers
According to this link, I think it's because I'm trying to call a non-const function which belongs to the element of the set
. In other words, the element of the set
is std::future<std::string>
and std::future<std::string>::get()
is non-const. This is why I get such an error.
If I'm right, does it mean that I can never declare a std::set<std::future>
because its get
is always unusable?
Here is my code:
set<future<string>> results;
results.insert(...); // insert some future
for(auto it = results.begin(); it != results.end();)
{
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
string tmp = it->get(); // ERROR!!!
}
}