473,836 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A problem of T * const &

the program is as follows:
#include <vector>
using namespace std;

class A{};
int main()
{
A* const &p = NULL;
vector<A*B(3,NU LL); //there is a compile error
B.push_back(NUL L);
return 0;
}

int VC7£¬the error is
d:\Microsoft Visual Studio .NET 2003\Vc7\includ e\vector(357): error
C2664: "std::vector<_T y>::_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
9 1859
A* const &p = NULL;
vector<A*B(3,NU LL); //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

<t.*******@rtsg roup.netschrieb im Newsbeitrag
news:11******** **************@ l12g2000cwl.goo glegroups.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,NU LL); //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

mi*********@gma il.com wrote:
the program is as follows:
#include <vector>
using namespace std;

class A{};
int main()
{
A* const &p = NULL;
vector<A*B(3,NU LL); //there is a compile error
B.push_back(NUL L);
return 0;
}

int VC7£¬the error is
d:\Microsoft Visual Studio .NET 2003\Vc7\includ e\vector(357): error
C2664: "std::vector<_T y>::_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
* Earl Purple:
mi*********@gma il.com wrote:
>the program is as follows:
#include <vector>
using namespace std;

class A{};
int main()
{
A* const &p = NULL;
vector<A*B(3,NU LL); //there is a compile error
B.push_back(NUL L);
return 0;
}

int VC7,the error is
d:\Microsoft Visual Studio .NET 2003\Vc7\includ e\vector(357): error
C2664: "std::vector<_T y>::_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

"Oliver Bleckmann дµÀ£º
"
<t.*******@rtsg roup.netschrieb im Newsbeitrag
news:11******** **************@ l12g2000cwl.goo glegroups.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,NU LL); //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
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
news:11******** **************@ l12g2000cwl.goo glegroups.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.*******@rtsg roup.netschrieb im Newsbeitrag
news:11******** **************@ l12g2000cwl.goo glegroups.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,NU LL); //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
mi*********@gma il.com:
vector<A*B(3,NU LL);

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

~~~~~~~~~~~~~th e 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*********@gma il.com wrote:
the program is as follows:
#include <vector>
using namespace std;

class A{};
int main()
{
A* const &p = NULL;
vector<A*B(3,NU LL); //there is a compile error
B.push_back(NUL L);
return 0;
}

int VC7,the error is
d:\Microsoft Visual Studio .NET 2003\Vc7\includ e\vector(357): error
C2664: "std::vector<_T y>::_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16249
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
16
2494
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
11
2587
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find the best match. Anyone could give a detail explanation in theory? Which one is good?
3
1887
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
6
15382
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to use if i dont want to change content of arg1 it in func body? -- << pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl << prompt$ :(){ :|:& };: << echo mail | sed 's/__NOSPAM//g'
0
1712
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it seems to be working fine now. >-----Original Message----- >I have C++ code that has been running just fine in VS6 for >a couple of years. I brought it into VS.NET with managed
2
2187
by: TonyM | last post by:
Q: Is there a good way to overcome this apparent bug without modifying the mfc code? ___________________________________ Info: Although it is NOT a good idea, I seemed to have perhaps located a bug in this function with the GetLength call whose results are sent to the sub-call to Concatenate. So I added the following and it asserts on particular strings that are used. (I am not sure if this makes any difference, but I am using class...
6
10059
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This little bit of code: SimS::SimS (ostream &s) {
1
2135
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
2
1732
by: dolphin | last post by:
Why should we use fun(const Class &B) instead of fun(Class &B)?
0
9671
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10852
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9383
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6980
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.