Consider this:
std::vector<int*> v(1, 0);
This compiles fine with VC++10 (no warnings even at max warning level). However, it doesn’t compile with llvm on mac or gcc on linux, giving an error like “assigning to int* from incompatible type const int.” I’m not looking for solutions — I know the second parameter is unnecessary or that a static_cast fixes the error.
I thought zero was implicitly convertible to any pointer type. What gives? I can do the following:
int* i = 0;
int* const& ii = 0;
const int t = 0;
i = t;
I understand that the vector constructor signature takes a const T& which when expanded for vector<int*> becomes int* const& correct? Can someone explain what is going on here, and whether the VC++ or non-VC++ compiler is correct?
It looks like g++ actually wrong here. See C++98 23.1.1/9:
Note that
InputIteratoris a template parameter to the constructor, which in this case will beintfor your example, and thus an integral type. The g++ library actually has specific code to handle all the cases where the type being stored in thevectoris integral as well and those will all work properly. In this case, only because you used0would thestatic_castdictated by the standard actually be legal. I tried compiling the code that the standard says should be equivalent and it compiles with g++ 4.5.