Consider the following C++ Program
#include<map>
#include<iostream>
int main() {
int a = 5, b = 7;
auto pair = std::make_pair<int, int>(a,b);
return 0;
}
Using VC11 and also in gcc-4.7.2 fails with different errors, though it seems to be related and VC11 error message is more meaningful
You cannot bind an lvalue to an rvalue
What I understand from this failure is
- VC11 and I suppose gcc-4.7.2 have only one implementation of std::make_pair
make_pair(_Ty1&& _Val1, const _Ty2& _Val2)which can accept only an rvalue reference. Prior VC++ version example VC10 had two versions, one to accept an lvalue and another an rvalue reference - Rvalue reference cannot be used to initialize a non const reference i.e.
int & a = b * 5is invalid. - I could have used
std::moveto convert thelvaluetorvaluereference and the call would be successful. - As
std::make_pairaccepts two different types for each of the parameters, Template Argument Resolution in all possible cases can resolve the type of the parameter and an explicitly specifying the type is not required.
This scenario seems trivial and the incompatibility can easily be resolved by removing the explicit type specification and making the definition as
auto pair = std::make_pair(a,b);
- Now, my question is, what is the driving factor to remove the lvalue implementation from the library?
- Is it possible to know any other library functions which has been changed in similar ways?
- How to handle these situations when I need to target multiple compilers like g++, CC, aCC, XL C++ where either the compilers have not been upgraded or the compiler isn’t supporting the rvalue reference and or move semantics.
std::make_pairexists for the sole purpose of exploiting type deduction to avoid typing the names of the types. That’s why there is only one overload (and it should take two universal references, not one universal reference and an lvalue reference to const as VC seems to think).If you want to type the types explicitly, you can just use the pair constructor. It is even shorter…