Вопрос

Я недавно установил Visual Studio 11 Developer Preview.Во время игры с нитями и фьючерсами я пришел вокруг этой настройки:

#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();
}
.

Так, очень просто.Но поскольку есть два аргумента для «Foo», VS 11 не хочет его компилировать.(Тем не менее, G ++ делает: http://ideone.com/anrpj ) (ошибка времени выполнения не проблема: std :: будущее исключение на экспериментальную реализацию GCC C ++ 0x ) (VS 11 Errormessage: http://pastebin.com/f9xunh2s )

Теперь я немного запутался прямо сейчас, поскольку эта ошибка кажется мне чрезвычайно очевидной для меня, даже если это предпросмотр разработчика.Так что мои вопросы:

    .
  • это правильный код в соответствии со стандартом C ++ 11?
  • Это ошибка уже известна / сообщает?
Это было полезно?

Решение

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)

Другие советы

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).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top