3

please consider the code below:

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;
}

I'm trying to construct a tuple from a move-only type with a std::funtion<void()> member. Under Visual Studio 2017 this code doesn't compile:

 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

If i make the copy constructor = default everything works fine. I know this is related to the std::function, but I'm not sure what is the problem.

Thank you.

IT_
  • 63
  • 5

1 Answers1

1

This is apparently an implementation bug: there's no initializer list in your code at all, and current versions of all compilers accept it (MSVC since v19.22).

Davis Herring
  • 36,443
  • 4
  • 48
  • 76