Simple question, why doesn't the following work (implying a copy of ci
)?
#include <utility>
int main(){
const int ci = 2;
std::forward<int>(ci);
}
prog.cpp: In function 'int main()':
prog.cpp:6:23: error: no matching function for call to 'forward(const int&)'
The problem manifested itself while writing some template stuff, where I have a simple holder type as follows. To avoid unnecessary copies, I use perfect forwarding where possible, but that turns out to be the root of the problem it seems.
template<class T>
struct holder{
T value;
holder(T&& val)
: value(std::forward<T>(val))
{}
};
template<class T>
holder<T> hold(T&& val){
// T will be deduced as int, because literal `5` is a prvalue
// which can be bound to `int&&`
return holder<T>(std::forward<T>(val));
}
template<class T>
void foo(holder<T> const& h)
{
std::tuple<T> t; // contrived, actual function takes more parameters
std::get<0>(t) = std::forward<T>(h.value); // h.value is `const T`
}
int main(){
foo(hold(5));
}
If any further information is needed, please let me know.
Any idea to circumvent this problem is greatly appreciated.