Connecting Tech Pros Worldwide Forums | Help | Site Map

A problem of T * const &

miaohua1982@gmail.com
Guest
 
Posts: n/a
#1: Nov 30 '06
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.lehmann@rtsgroup.net
Guest
 
Posts: n/a
#2: Nov 30 '06

re: A problem of T * const &


A* const &p = NULL;
Quote:
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!)

Oliver Bleckmann
Guest
 
Posts: n/a
#3: Nov 30 '06

re: A problem of T * const &



<t.lehmann@rtsgroup.netschrieb im Newsbeitrag
news:1164880350.245305.255410@l12g2000cwl.googlegr oups.com...
Quote:
Quote:
> A* const &p = NULL;
what's this? call by reference which is null assigned const on a A* what
ever...
Quote:
Quote:
> 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...


Earl Purple
Guest
 
Posts: n/a
#4: Nov 30 '06

re: A problem of T * const &



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

Alf P. Steinbach
Guest
 
Posts: n/a
#5: Nov 30 '06

re: A problem of T * const &


* 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?
miaohua1982@gmail.com
Guest
 
Posts: n/a
#6: Nov 30 '06

re: A problem of T * const &



"Oliver Bleckmann дµÀ£º
"
Quote:
<t.lehmann@rtsgroup.netschrieb im Newsbeitrag
news:1164880350.245305.255410@l12g2000cwl.googlegr oups.com...
Quote:
Quote:
A* const &p = NULL;
what's this? call by reference which is null assigned const on a A* what
ever...
Quote:
Quote:
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...
Frederick Gotham
Guest
 
Posts: n/a
#7: Nov 30 '06

re: A problem of T * const &


Earl Purple:
Quote:
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
miaohua1982@gmail.com
Guest
 
Posts: n/a
#8: Nov 30 '06

re: A problem of T * const &


news:1164880350.245305.255410@l12g2000cwl.googlegr oups.com...
Quote:
Quote:
Quote:
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 дµÀ£º
"
Quote:
<t.lehmann@rtsgroup.netschrieb im Newsbeitrag
news:1164880350.245305.255410@l12g2000cwl.googlegr oups.com...
Quote:
Quote:
A* const &p = NULL;
what's this? call by reference which is null assigned const on a A* what
ever...
Quote:
Quote:
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...
Frederick Gotham
Guest
 
Posts: n/a
#9: Nov 30 '06

re: A problem of T * const &


miaohua1982@gmail.com:
Quote:
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
miaohua1982@gmail.com
Guest
 
Posts: n/a
#10: Nov 30 '06

re: A problem of T * const &


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?
Closed Thread