![](/img/trans.png)
[英]Is `std::array<T, 0>` default constructible where `T` is not default constructible?
[英]std::promise<T> where T must be default constructible in Visual Studio 2017?
我正在嘗試在 Visual Studio 2017 中編譯以下代碼:
#include <future>
int main()
{
std::promise<std::reference_wrapper<int>> promise;
(void)promise;
}
但是,我收到以下錯誤:
錯誤 C2512:“std::reference_wrapper”:沒有合適的默認構造函數可用
而它與 GCC 和 Clang 編譯得很好。
這是 Visual Studio 中的一個明確錯誤還是 std::promise 的有效實現?
看起來這是 MSVC 標准庫實現中的一個已知問題。 一個更簡單的再現場景:
#include <future>
struct NoDefaultCtor
{
NoDefaultCtor() = delete;
};
int main() {
std::promise<NoDefaultCtor> p;
return 0;
}
我想你不需要std::reference_wrapper<int>
。 有適用於std::promise
重載模板可用:
template<class R> class promise<R&>;
因此,您可以在 Visual Studio 2017 中修復您的代碼:
#include <future>
int main()
{
std::promise<int&> promise;
(void)promise;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.