By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

A problem of T * const &

P: n/a
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 VC7the 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?!

Nov 30 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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!)

Nov 30 '06 #2

P: n/a

<t.*******@rtsgroup.netschrieb im Newsbeitrag
news:11**********************@l12g2000cwl.googlegr oups.com...
> A* const &p = NULL;
what's this? call by reference which is null assigned const on a A* what
ever...
> 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);
well an (A*)NULL looks like a c-style typecast to me, hm?
maybe the fist line is off use...
Nov 30 '06 #3

P: n/a

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 VC7the 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).

Nov 30 '06 #4

P: n/a
* 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?
Nov 30 '06 #5

P: n/a

"Oliver Bleckmann д
"
<t.*******@rtsgroup.netschrieb im Newsbeitrag
news:11**********************@l12g2000cwl.googlegr oups.com...
A* const &p = NULL;
what's this? call by reference which is null assigned const on a A* what
ever...
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);
well an (A*)NULL looks like a c-style typecast to me, hm?
maybe the fist line is off use...
Nov 30 '06 #6

P: n/a
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.

--

Frederick Gotham
Nov 30 '06 #7

P: n/a
news:11**********************@l12g2000cwl.googlegr oups.com...
A* const &p = NULL;
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 д
"
<t.*******@rtsgroup.netschrieb im Newsbeitrag
news:11**********************@l12g2000cwl.googlegr oups.com...
A* const &p = NULL;
what's this? call by reference which is null assigned const on a A* what
ever...
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);
well an (A*)NULL looks like a c-style typecast to me, hm?
maybe the fist line is off use...
Nov 30 '06 #8

P: n/a
mi*********@gmail.com:
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*.

--

Frederick Gotham
Nov 30 '06 #9

P: n/a
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:
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?
Nov 30 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.