I read this (incredibly well written) article about Forwarding Reference in C++11 by Scott Meyers.
Now, focus on this part of the article:
template <class... Args> void emplace_back(Args&&... args); // deduced parameter types ⇒ type deduction; ... // && ≡ universal references
So, in contrast with other cases, the ellipses doesn't make the && an rvalue reference, but it's still universal references.
From what I've understood, when we have universal references, we can call the function passing both rvalue and lvalues (wow, so cool!)
Now, I've implemented this function:
template <typename ReturnType, typename... Args>
ReturnType callFunction(MemFunc<ReturnType, Args...> memFunc, Args&& ... args) { ...
So (using the same logic of the previous example), && means forwarding references.
But if I try to make this call:
typedef vector<double> vecD;
vecD vec;
mem.callFunction<vecD, vecD>(sortFunc, vec);
The compiler is going to complain with You cannot bind an lvalue to an rvalue reference
Why this happens?
THE WHOLE CODE:
#include <functional>
#include <vector>
using namespace std;
struct MultiMemoizator {
template <typename ReturnType, typename... Args>
ReturnType callFunction(std::function<ReturnType(Args...)> memFunc, Args&&... args) {
}
};
typedef vector<double> vecD;
vecD sort_vec (vecD const& vec) {
return vec;
}
int main()
{
vecD vec;
std::function<vecD(vecD)> sortFunc(sort_vec);
MultiMemoizator mem;
mem.callFunction<vecD, vecD>(sortFunc, vec);
}
Argsis actually deduced in your call? - Kerrek SBsort_vec?), and certainly isn't minimal. - Barry