String literal pointer decay funkiness... : c++
This is a discussion on String literal pointer decay funkiness... within the c++ forums in Programming Languages category; 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 ...
| c++ comp.lang.c++ usenet archive |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| 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 ] |
|
#2
| |||
| |||
| Frederick Gotham wrote: > The type of a string literal is: > > char const[sizeof "whatever is written"] This is true. > 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. ] > 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 ] |
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Display literal string | usenet | CSharp | 2 | 12-12-2007 01:37 PM |
| Literal string | usenet | CSharp | 5 | 10-19-2007 12:15 PM |
| String literal encoding - What can it be? | usenet | c++ | 2 | 03-25-2007 11:09 AM |
| Defect Report: "string-literal" used where narrow string literal is possibly intended | usenet | c++ | 3 | 09-13-2006 08:40 AM |
| Re: Premature Array-to-Pointer Decay | usenet | c++ | 5 | 05-18-2006 03:19 PM |


