Question

I recently installed the Visual Studio 11 Developer Preview. While playing with threads and futures, I came around this setup:

#include <future>
#include <iostream>

int foo(unsigned a, unsigned b)
{
    return 5;
}

int main()
{
    std::future<int> f = std::async(foo, 5, 7);
    std::cout << f.get();
}

So, very simple. But since there are two arguments for "foo", VS 11 doesn't want to compile it. (However, g++ does: http://ideone.com/ANrPj) (The runtime error is no problem: std::future exception on gcc experimental implementation of C++0x) (VS 11 errormessage: http://pastebin.com/F9Xunh2s)

I'm a little confused right now, since this error seems extremely obvious to me, even if it is a developer preview. So my questions are:

  • Is this code correct according to the C++11 standard?
  • Is this bug already known/reported?
Was it helpful?

Solution

Try below adhoc workaround. (I tried at Visual Studio 11 Beta)

std::future<int> f = std::async(std::launch::any, foo, 5, 7);

As C++11 standards function std::async() has two overloads, but MSVC/CRT can't do correct overload resolution. Furthermore std::launch::any is NOT a part of standard. (it requires std::launch::async|std::launch::deferred, but they can't compile again)

OTHER TIPS

std::future is supposed to be a variadic template. This is what allows you to pass an arbitrary number of arguments to the function being invoked asynchronously.

Unfortunately, the current preview of VS 11 doesn't support variadic templates, which means it doesn't have the mechanism for you to pass more than one argument to the function.

Bottom line: VS is wrong. I'm not sure if anybody has reported this as a bug, but it's a direct consequence of a fact that's already well known, so reporting it probably wouldn't/won't do a whole lot of good other than indirectly adding a vote that variadic templates are important.

If you look on the VC++ News page, they (currently) have a link to a survey that's supposed to allow you to indicate priorities you'd assign to conformance with various C++11 features. Unfortunately it seems to be offline, at least at the moment. When you can, filling it in to indicate that you consider variadic templates a high priority has at least some chance of doing some good for this (though I obviously can't guarantee anything).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top