I was trying to compile this code but it fails on C++14 while it works on C++17
#include <cstdio>
#include <utility>
template <typename F>
struct S {
explicit S(F&& fn): fn(std::move(fn)) {}
F fn;
~S() { fn(); }
};
int main(){
S obj([]() noexcept {
std::printf("Foo\n");
});
}
I see that in C++17 the constructor of S has been invoked as
call S<main::{lambda()#1}>::S(main::{lambda()#1}&&)
which indicates that the compiler deduced the template argument. Is there a way to get this code compiling in C++14 other than doing something as follows?
auto fn = []() noexcept {
std::printf("Foo\n");
};
S<decltype(fn)> obj(std::move(fn));