【问题标题】:Constructing a tuple from a move only type with a std::function member使用 std::function 成员从仅移动类型构造元组
【发布时间】:2020-08-29 17:26:07
【问题描述】:

请考虑以下代码:

struct no_copy {
int x_;
std::function<void()> f_;

template <typename F>
no_copy(int x, F&& f)
    : x_{ x }, f_{ std::forward<F>(f) } {};

no_copy(const no_copy&) = delete;
no_copy& operator= (const no_copy&)  = delete;

no_copy(no_copy&&) noexcept = default;
no_copy& operator= (no_copy&&) noexcept = default;
};

int main() {
auto nc = no_copy(1, []() {});
auto tuple_func = std::make_tuple(std::move(nc), 2);

return 0;
}

我正在尝试从具有std::funtion&lt;void()&gt; 成员的仅移动类型构造一个元组。在 Visual Studio 2017 下,此代码无法编译:

 error C2440: '<function-style-cast>': cannot convert from 'initializer list' to '_Ttype'
 note: No constructor could take the source type, or constructor overload resolution was ambiguous

如果我使复制构造函数= default 一切正常。我知道这与std::function 有关,但我不确定是什么问题。

谢谢。

【问题讨论】:

  • MSVC 2017 是有史以来漏洞最多的 MSVC。尽快升级到 2019 年。

标签: c++ stdtuple


【解决方案1】:

这显然是一个实现错误:您的代码中根本没有初始化列表,所有编译器的当前版本accept it(MSVC 自 v19.22 起)。

【讨论】:

    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2019-07-06
    • 2014-10-09
    相关资源
    最近更新 更多