C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What is the"standard "answer to this program piece something

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sciwander
Guest





PostPosted: Sat Feb 18, 2006 5:06 am    Post subject: What is the"standard "answer to this program piece something Reply with quote



#include <iostream>
//using namespace std;
//const_match.cpp
#include <iostream>
using namespace std;

void f(const int* & p) //#1
{
int a=2;
int* p1=&a;
p=p1;
cout<<"#1:"<<p<<endl;
}
void f(const void* p) //#2
{
int a=2;
int* p2=&a;
p=p2;
cout<<"#2:"<<p<<endl;
}

void foo(const int* p)
{
cout << "A" << endl;
}
void foo(int* const p)
{
cout << "B" << endl;
}

typedef int* int_ptr;
void goo(const int* p)
{
cout << "A" << endl;
}
void goo(const int_ptr p)
{
cout << "B" << endl;
}

template<class T>
void ff(const T val)
{
foo(val);
goo(val);
}

int main(int argc, char *argv[])
{
int* p=0;
f( p );
const int* const& r = p;
cout<<p<<" "<<r<<endl;
ff(p);
return 0;
}
in G++ 3.1 I got :#2 0x22ff54
0 0
B
B
but In VC2003 The answer is #1 : 0012FDE1
0012FDE1 0012FDE1
B
B
why an (int *) implicited converted to (const void*) not the (const
int*)?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Sun Feb 19, 2006 12:06 pm    Post subject: Re: What is the"standard "answer to this program piece somet Reply with quote



By the way, Visual Studio 2005 agrees with G++ 3.1. I believe that VS
2003 is incorrect. The reason why (int*) is converted to (const void*)
is that the first overload to f requires a conversion to (const int*&)
-- notice the reference -- instead of (const int*).

If I comment out the second overload, VS20005 produces and error during
compilation:

error C2664: 'f' : cannot convert parameter 1 from 'int *' to
'const int *&'

However, if you remove the reference so that the first overload is:

void f(const int* p) //#1

then, VS2005 will choose answer #1. And this makes sense. (int*) will
be converted to (const int*) before being converted to (const void*).


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Mon Feb 20, 2006 1:06 pm    Post subject: Re: What is the"standard "answer to this program piece somet Reply with quote



sciwander wrote:
Quote:
#include <iostream
//using namespace std;
//const_match.cpp
#include <iostream
using namespace std;

void f(const int* & p) //#1

Attention: the parameter here is a reference to a non-const,
which means that the function can only be called with an lvalue.
(Many compilers do not yet enforce this, although the rule dates
from over 15 years ago.)

Quote:
{
int a=2;
int* p1=&a;
p=p1;
cout<<"#1:"<<p<<endl;
}

void f(const void* p) //#2

Here, it's call by value (not by reference). You pass a copy,
so there is no problem with conversion.

[...]

Quote:
int main(int argc, char *argv[])
{
int* p=0;
f( p );

And here, you call f with an int*. There are no functions which
take an int*, so we need a conversion. The results of a
conversion are not an lvalue, and so cannot be bound to a
non-const reference.

Quote:
const int* const& r = p;

Here, there is a reference to a const, so we can bind to the
results of the conversion. Later modifications to p should not
be takent into account by r. Try the following, for example:

int
main()
{
int x = 42, y = 24 ;
int* p = &x ;
int const* const& r = p ;
p = &y ;
std::cout << *p << ',' << *r << std::endl ;
return 0 ;
}

Now try the same thing using an int const* const* instead of a
reference, and explicitly taking the address and
dereferencing:-). (A sort of a wierd case where references
cannot be described in terms of automatically dereferenced
pointers.)

Quote:
cout<<p<<" "<<r<<endl;
ff(p);
return 0;
}
in G++ 3.1 I got :#2 0x22ff54
0 0
B
B
but In VC2003 The answer is #1 : 0012FDE1
0012FDE1 0012FDE1
B
B
why an (int *) implicited converted to (const void*) not the
(const int*)?

The int* can be implicitly converted to either. The problem is
that the results of the implicit conversion cannot be bound to a
non-const reference. Since the compiler cannot legally call
f(int const*&), the function isn't taken into consideration
during overload resolution.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.