Topic: type of "hello" : const char[6] or char[6]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/11/02 Raw View
Siemel Naran<sbnaran@KILL.uiuc.edu> wrote:
>The type of "hello" should be 'const char [6]'.
No, the true situation is more complicated.
>I have a template function
>template <int N> void f(const char (&)[N]);
>
>Since no conversions are performed when calling a template function,
>it follows that a type 'char [6]' will not be promoted to
>'const char [6]'.
That is also not really true. non-const to const conversions are
supposed to be considered. (This is a change from the ARM.) Still...
>It appears that "hello" actually has type 'char [6]'. Hence, the
>call to f(...) fails!
That's a bug. The call f("hello") should succeed, and deduce N=6.
In practice, the type of "hello" is really neither 'char[6]' nor
'const char[6]', but rather a third, unnameable, type that might
be described as 'literal char[6]', which converts to const char[6],
const char*, or char*, but prefers the const ones. It should not
match or convert to char[6].
What it says in 2.13.4 (lex.string) is:
An ordinary string literal has type ``array of n const
char'' and static storage duration (basic.stc)...
In 4.2 (conv.array) it adds:
-2- A string literal (lex.string) ...
can be converted to an rvalue of type ``pointer to char''; ...
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 depr. ] For the purpose of ranking in overload resolution
(over.ics.scs), this conversion is considered an array-to-pointer
conversion followed by a qualification conversion (conv.qual).
[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. ]
So its type cannot *really* be a regular const array, because for the
purpose of conversions it has to do strange (but deprecated) things
a regular const array won't. But anyway your template should
match it properly.
This is a somewhat strange and lately-made rule, and it is not widely
implemented yet.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/11/02 Raw View
In article <slrn6uk7mp.8o0.sbnaran@localhost.localdomain>, Siemel Naran
<sbnaran@localhost.localdomain> writes
>The type of "hello" should be 'const char [6]'.
And I think it does, but few compilers have caught up with the subtle
change introduced a couple of years ago.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/11/02 Raw View
In article <cPb%1.1$eI4.22133@brnws01.ne.mediaone.net>, Christopher M.
Gurnee <gurnec_at_mediaone_dot_net@127.0.0.1> writes
>Did string literals used to be of type char[N]?
until about two years ago and they still are in C.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/11/02 Raw View
The type of "hello" should be 'const char [6]'.
I have a template function
template <int N> void f(const char (&)[N]);
Since no conversions are performed when calling a template function,
it follows that a type 'char [6]' will not be promoted to
'const char [6]'.
It appears that "hello" actually has type 'char [6]'. Hence, the
call to f(...) fails! Why is the rule like this? Is it possible to
change the rule?
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Christopher M. Gurnee" <gurnec_at_mediaone_dot_net@127.0.0.1>
Date: 1998/11/02 Raw View
Siemel Naran wrote in message ...
>The type of "hello" should be 'const char [6]'.
You're right, it is.
>I have a template function
>template <int N> void f(const char (&)[N]);
>
>Since no conversions are performed when calling a template function,
>it follows that a type 'char [6]' will not be promoted to
>'const char [6]'.
>
>It appears that "hello" actually has type 'char [6]'. Hence, the
>call to f(...) fails! Why is the rule like this? Is it possible to
>change the rule?
The call to f shouldn't fail, your compiler is broken. You should also
note that there is a deprecated rule in the standard that says a string
literal can be converted to char* (a standard conversion) even though
its type is const char[N].
Did string literals used to be of type char[N]?
I checked two compilers on the issue.
Egcs 1.1 is broken, even though the typeid's of these two:
"Hello";
const char str[] = "Hello";
are the same. Calling f(a) does work. I guess the conversion is done
by Egcs even though it's not required (or permitted).
VC5 SP3 has the same state of affairs, plus it doesn't even like the
declaration of f, complaining that N is a non-const expression :P
-Chris Gurnee
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]