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 

String literal pointer decay funkiness...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Frederick Gotham
Guest





PostPosted: Tue Oct 03, 2006 9:08 pm    Post subject: String literal pointer decay funkiness... Reply with quote



The type of a string literal is:

char const[sizeof "whatever is written"]

However, it decays to a pointer to non-const, rather than a pointer to const.

Here's some funky code:

void Func(char*) {}
void Func(char const*) {}

int main()
{
Func("Hello");
}

I would have thought that the "char*" version would be invoked, but it seems
that at least one modern compiler invokes the "char const*" version.

Any thoughts?

--

Frederick Gotham

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Ron Natalie
Guest





PostPosted: Wed Oct 04, 2006 2:40 am    Post subject: Re: String literal pointer decay funkiness... Reply with quote



Frederick Gotham wrote:
Quote:
The type of a string literal is:

char const[sizeof "whatever is written"]

This is true.

Quote:
However, it decays to a pointer to non-const, rather than a pointer
to const.


This is not true:

A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type “pointer to
char”; a wide string literal can be converted to an rvalue of type
“pointer to wchar_t”. In either case,
the result is a pointer to the first element of the array. This
conversion is considered only when there is an
explicit appropriate pointer target type, and not when there is a
general need to convert from an lvalue to an
rvalue. [Note: this conversion is deprecated. See Annex D. ] For the
purpose of ranking in overload resolution
(13.3.3.1.1), this conversion is considered an array-to-pointer
conversion followed by a qualification
conversion (4.4). [Example: "abc" is converted to “pointer to const
char” as an array-to-pointer conversion,
and then to “pointer to char” as a qualification conversion. ]


Quote:
I would have thought that the "char*" version would be invoked, but it seems
that at least one modern compiler invokes the "char const*" version.

Why would you expect that? While the conversion to char* is permitted,

the type is still char const[n] which would match preferentially the
char const* overload.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards 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.