Can't figure out what is the difference between h1
and h2
in the code below:
#include <utility>
template <class Func>
struct Holder
{
Holder(Func && func) : m_func(std::forward<Func>(func))
{
}
Func m_func;
};
template <class Func>
auto MakeHolder(Func && func)
{
return Holder<Func>(std::forward<Func>(func));
}
int main()
{
auto func = [](int val) {};
Holder h1 = MakeHolder(func);
Holder<decltype(func)> h2(func);
return 0;
}
Why h1
compiles, but h2
does not? The error with GCC is
prog.cc:25 : 31 : error : cannot bind rvalue reference of type 'main()::<lambda(int)>&&' to lvalue of type 'main()::<lambda(int)>'
25 | Holder<decltype(func)> h2(func);
Are types Func
in the function template arguments and decltype<func>
in main()
different?