Thank you!Maybe I've understand.
Form cpp2003 23.1.1./10
[Note: This follows directly from the requirements in the Iterator
Requirements Table. Integral types cannot be iterators, so, if n1 and
n2 are values of an integral type N, the expression X(n1, n2) cannot
possibly be interpreted as construction from a range of iterators. It
must be taken to mean the first constructor in the Iterator
Requirements Table, not the second one. If there is no conversion from
N to X::value_type, then this is not a valid
~~~~~~~~~~~~~the NULL is define ,so the "N" is int ,int can't convert
to A*. Am I right??
expression at all.
"Alf P. Steinbach дµÀ£º
"
Quote:
* Earl Purple:
Quote:
miaohua1982@gmail.com wrote:
Quote:
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?