I tried to implement it as the following, with all of auto
, decltype()
and std::forward<>()
.
Is there any simpler implementation?
#include <vector>
using namespace std;
template<typename T>
auto foo(T &&t) -> decltype(t) {
return std::forward<T>(t);
}
int main() {
vector<int> v{0, 1};
vector<int> v0 = foo(v); // OK, copy ctor
vector<int> &v1 = foo(v); // OK, reference
vector<int> v2 = foo(vector<int>{0, 1}); // OK, ctor with rvalue
// vector<int> &v3 = foo(vector<int>{0, 1}); // Error, as expected
return 0;
}