从具有 std::function 成员的仅移动类型构造元组
Constructing a tuple from a move only type with a std::function member
请考虑以下代码:
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<void()>
成员的仅移动类型构造一个元组。在 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
有关,但我不确定是什么问题。
谢谢。
这显然是一个实现错误:您的代码中根本没有初始化列表,所有编译器的当前版本accept it(自 v19.22 起的 MSVC)。
请考虑以下代码:
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<void()>
成员的仅移动类型构造一个元组。在 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
有关,但我不确定是什么问题。
谢谢。
这显然是一个实现错误:您的代码中根本没有初始化列表,所有编译器的当前版本accept it(自 v19.22 起的 MSVC)。