问题描述
请考虑以下代码:
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)。