* Earl Purple:
mi*********@gmail.com wrote:
>the program is as follows:
#include <vector>
using namespace std;
class A{};
int main()
{
A* const &p = NULL;
vector<A*B(3,NULL); //there is a compile error
B.push_back(NULL);
return 0;
}
int VC7,the error is
d:\Microsoft Visual Studio .NET 2003\Vc7\include\vector(357): error
C2664: "std::vector<_Ty>::_Construct_n" : can't convert from "int"
to "A *const & "
with
[
_Ty=A *
]
which puzzles me is that , why this one"A* const &p = NULL;" is OK,
the compiler even doesn't give a warning?!
The error is not in the line you have indicated, it is in the one
above. A reference must be bound to a variable, and p is not.
It's like writing doing
int const & x = 0;
which is not valid as 0 is not a variable.
It's valid. A reference to const can be bound to an rvalue.
The error is in using plain 0 (which is what NULL is, NULL is not a
pointer) as a default value.
Some compilers may accept this (MSVC 7.1 does) but that's just bad luck.
Because the standard requires in §23.1.1/9 that a call of the
templated constructor taking two iterators shall have the same effect as
that call with the arguments casted to the vector's size_type, if the
iterator type is an integral type, and when NULL is defined simply as 0
instead of as 0L the above ends up in that case with the iterator type
as int. To do it properly, cast the default value to A*, like 'B(3,
(A*)NULL)'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?