简体   繁体   English

标准::承诺<T>在 Visual Studio 2017 中,T 必须是默认可构造的吗?

[英]std::promise<T> where T must be default constructible in Visual Studio 2017?

I am trying to compile the following code in Visual Studio 2017:我正在尝试在 Visual Studio 2017 中编译以下代码:

#include <future>

int main()
{
    std::promise<std::reference_wrapper<int>> promise;
    (void)promise;
}

However, I get the following error:但是,我收到以下错误:

error C2512: 'std::reference_wrapper': no appropriate default constructor available错误 C2512:“std::reference_wrapper”:没有合适的默认构造函数可用

Whereas it compiles fine with GCC and Clang.而它与 GCC 和 Clang 编译得很好。

Is this is a definite bug in Visual Studio or is it a valid implementation of std::promise?这是 Visual Studio 中的一个明确错误还是 std::promise 的有效实现?

Looks like it is a known issue in MSVC's standard library implementation.看起来这是 MSVC 标准库实现中的一个已知问题 A simpler reproduction scenario:一个更简单的再现场景:

#include <future>
struct NoDefaultCtor
{
    NoDefaultCtor() = delete;
};
int main() {
    std::promise<NoDefaultCtor> p;
    return 0;
}

I suppose you do not need std::reference_wrapper<int> .我想你不需要std::reference_wrapper<int> There is the suitable overloaded template for std::promise available:有适用于std::promise重载模板可用:

template<class R> class promise<R&>;

Therefore you can fix your code in Visual Studio 2017:因此,您可以在 Visual Studio 2017 中修复您的代码:

#include <future>

int main()
{
    std::promise<int&> promise;
    (void)promise;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是`std :: array <T, 0> `默认可构造的,而`T`不是默认可构造的? - Is `std::array<T, 0>` default constructible where `T` is not default constructible? 如何初始化std :: array <T, 2> 其中T是不可复制的,非默认可构造的? - How to initialize an std::array<T, 2> where T is non-copyable and non-default-constructible? 我们可以假设 std::is_default_constructible<t> 和 std::is_constructible<t> 平等吗?</t></t> - Can we assume std::is_default_constructible<T> and std::is_constructible<T> to be equal? 如何初始化 std::array<T, n> 如果 T 不是默认可构造的,那么优雅吗? - How to initialize std::array<T, n> elegantly if T is not default constructible? Visual Studio 2017找不到std :: variant - Visual Studio 2017 can't find std::variant 如果有 std::thread,Visual Studio 2017 linux 将无法编译 - Visual Studio 2017 linux can't compile if there's std::thread std :: is_default_constructible <T> 错误,如果构造函数是私有的 - std::is_default_constructible<T> error, if constructor is private 初始化std :: array <T,N> 当T不是默认可构造的时,在构造函数初始化器列表中 - Initialization of std::array<T,N> in constructor initializer list when T is not default-constructible nullptr_t是默认的可构造类型吗? - Is nullptr_t a default constructible type? 如何测试一个std :: function <T> 对于模板参数T是可构造的 - How to test if an std::function<T> is constructible for template argument T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM