Let me dive in C++14 generic lambdas with:
#include <iostream>
// g++ -std=c++14
template<typename T>
T incr(T v)
{
return v + 1;
}
int main()
{
float f = 2.0;
int i = 3;
auto selfincr = [] (auto & value)
{
value = incr<std::remove_reference<decltype(value)>>(value); // A
value = incr<decltype(value)>(value); // B
};
selfincr(f);
selfincr(i);
std::cout << "f " << f << ", i " << i << std::endl;
return 0;
}
Since line // B causes a
invalid initialization of non-const reference of type ‘T&’ from an rvalue of type ‘T’
My immediate guessing has been the removal of the reference, so I added line // A. But this yield a
no matching function for call to ‘incr(T&)’
So how could I remove that reference ?
So how could I remove that reference ?
incr<std::remove_reference<decltype(value)>>(value), you're specifying std::remove_reference<T> as the template parameter, but not the type referred by T (i.e. decltype(value)). What you want should be
value = incr<typename std::remove_reference<decltype(value)>::type>(value); // A
// ~~~~~~~~ ~~~~~~
And since C++14 you could make it simpler:
value = incr<std::remove_reference_t<decltype(value)>>(value); // A
// ~~
LIVE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With