A problem of T * const &

M

miaohua1982

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?!
 
T

t.lehmann

A* const &p = NULL;
vector<A*> B(3,NULL); //there is a compile error

Short: Well, "NULL" is not of value_type "A*"!
Just write: vector<A*> B(3,(A*)NULL);

Long: I'm not an C++ standard guru but I guess because the vector (a
template) isn't created yet the compiler tries to match the correct
ctor but doesn't find it because 'NULL' will be treaded as type int
(see your warning)! (Is a difference to a class myvector - no template
- with a ctor myvector(A*) throwing no warning!)
 
O

Oliver Bleckmann

what's this? call by reference which is null assigned const on a A* what
ever...
Short: Well, "NULL" is not of value_type "A*"!
Just write: vector<A*> B(3,(A*)NULL);
well an (A*)NULL looks like a c-style typecast to me, hm?
maybe the fist line is off use...
 
E

Earl Purple

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. Perhaps the fact that it is
const (and therefore does not need to be an l-value) has confused you.

(Compilers ranting on about "int" for undefined types by the way is one
of my biggest annoyances. "Literal" would be better because NULL which
is 0 is also a pointer).
 
A

Alf P. Steinbach

* Earl Purple:
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)'.
 
M

miaohua1982

"Oliver Bleckmann дµÀ£º
"
what's this? call by reference which is null assigned const on a A* what
ever...
well an (A*)NULL looks like a c-style typecast to me, hm?
maybe the fist line is off use...
 
F

Frederick Gotham

Earl Purple:
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.


You might want to try compile that -- you'll see that a "reference to const"
can be bound to an R-value. The language provides special rules for this.
 
M

miaohua1982

what's this? call by reference which is null assigned const on a A* what
ever...
this is a "A*" const reference, and the "NULL" is defined as 0,so a
const reference to rvalue is valid

"Oliver Bleckmann дµÀ£º
"
 
F

Frederick Gotham

(e-mail address removed):
vector<A*> B(3,NULL);


The Standard necessitates that NULL must be a macro which expands to a
compile-time integer type whose value is 0. Examples are:

#define NULL 0

#define NULL 0L

#define NULL '\0'

#define NULL (6 - 4 - 2)

I don't know the exact templates rules, but it seems the compiler is unhappy
with having an integer type where it should have an A*.
 
M

miaohua1982

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 дµÀ£º
"
* Earl Purple:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top