473,657 Members | 2,535 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 1849
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
16235
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
2463
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
2524
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
1856
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
15321
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
1697
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
2175
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
10021
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
2114
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
2
1721
by: dolphin | last post by:
Why should we use fun(const Class &B) instead of fun(Class &B)?
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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...
1
6163
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
4151
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.