Topic: conversions for array size in new
Author: Pete Becker <petebecker@acm.org>
Date: 1997/06/15 Raw View
J. Stephen Adamczyk wrote:
>
> The WP used to say that conversions were done wherever necessary, but
> that statement was removed in favor of being explicit about the places
> where implicit conversions are allowed. There is no explicit statement
> about implicit conversions for new[], so they aren't allowed.
>
Was that an accident, or a deliberate decision?
-- Pete
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jsa@edg.com (J. Stephen Adamczyk)
Date: 1997/06/16 Raw View
In article <33A3EE45.59E4@acm.org> petebecker@acm.org writes:
>J. Stephen Adamczyk wrote:
>> The WP used to say that conversions were done wherever necessary, but
>> that statement was removed in favor of being explicit about the places
>> where implicit conversions are allowed. There is no explicit statement
>> about implicit conversions for new[], so they aren't allowed.
>
>Was that an accident, or a deliberate decision?
new[] wasn't discussed, so it can't be said to have been deliberate.
On the other hand, if it had been discussed, I personally would have
had no problem with leaving out the implicit conversions for new[].
Others may have differing opinions.
Steve Adamczyk
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Vidyasankar Raman <Vidyasankarx_Raman@ccm.jf.intel.com>
Date: 1997/06/11 Raw View
Tony Cook wrote:
>
> Are the functions f() and g() legal C++ code?
>
> int *f(float i)
> {
> return new int[i];
> }
>
> class A
> {
> public:
> operator int() const { return 5; }
> };
>
> int *g(A a)
> {
> return new int[a];
> }
>
> I get the following errors from one compiler:
>
> newarrf.cpp(3): Error! E529: col(20) expression for number of array
> elements must be integral
> newarrf.cpp(14): Error! E529: col(20) expression for number of array
> elements must be integral
>
> Are conversions done for the array size when calling new []?
>
> --
> Tony Cook - tony@online.tmx.com.au <= testing .procmailrc
> 100237.3425@compuserve.com
> ---
> [ comp.std.c++ is moderated. To submit articles: Try just posting with your
> newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
> comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
> Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
> Comments? mailto:std-c++-request@ncar.ucar.edu
> ]
Which compiler are you using ?. it compiles correctly Visual C++ 4.1
Vidya.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Justin" <g843839@oz.nthu.edu.tw>
Date: 1997/06/11 Raw View
Tony Cook <tony@online.tmx.com.au> wrote in article
<5ni584$jm2@news.tmx.com.au>...
> Are the functions f() and g() legal C++ code?
>
> int *f(float i)
> {
> return new int[i];
> }
>
> class A
> {
> public:
> operator int() const { return 5; }
> };
>
> int *g(A a)
> {
> return new int[a];
> }
>
> I get the following errors from one compiler:
>
> newarrf.cpp(3): Error! E529: col(20) expression for number of array
> elements must be integral
> newarrf.cpp(14): Error! E529: col(20) expression for number of array
> elements must be integral
>
> Are conversions done for the array size when calling new
The syntax of function f() is certainly wrong, but it seems that the syntax
of function g() should be workable. I guess
that the compiler will not implicitly call the convention
function for class in expression of number of array.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/06/11 Raw View
Tony Cook writes:
> Are conversions done for the array size when calling new []?
Nope. In paragraph 7 of [expr.new], in the CD2, there's a requirement
that expressions in a direct-new-declarator must have integral type.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Steve Clamage <stephen.clamage@Eng.Sun.COM>
Date: 1997/06/11 Raw View
Tony Cook wrote:
>
> Are the functions f() and g() legal C++ code? ...
>
> Are conversions done for the array size when calling new []?
No. Section 5.3.4 "New" requires that the expression in
brackets have integral type.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1997/06/12 Raw View
Steve Clamage (stephen.clamage@Eng.Sun.COM) wrote:
: Tony Cook wrote:
: >
: > Are the functions f() and g() legal C++ code? ...
: >
: > Are conversions done for the array size when calling new []?
: No. Section 5.3.4 "New" requires that the expression in
: brackets have integral type.
The same is true for the subscript expression itself:
5.2.1 Subscripting [expr.sub]
1 A postfix expression followed by an expression in square brackets is a
postfix expression. One of the expressions shall have the type
"pointer to T" and the other shall have enumeration or integral type.
The result is an lvalue of type "T." The type "T" shall be a com-
pletely-defined object type.3) The expression E1[E2] is identical (by
definition) to *((E1)+(E2)). [Note: see _expr.unary_ and _expr.add_
for details of * and + and _dcl.array_ for details of arrays. ]
(except that it explicitly allows enumeration types too)
Does this mean that a subscript expression doesn't allow conversions
either?
--
Tony Cook - tony@online.tmx.com.au <= testing .procmailrc
100237.3425@compuserve.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jim.hyslop@leitch.com (Jim Hyslop)
Date: 1997/06/13 Raw View
In article <339ED8C8.7AB6@Eng.Sun.COM> on 11 Jun 1997 15:15:22 PDT,
stephen.clamage@Eng.Sun.COM says...
> Tony Cook wrote:
> >
> > Are the functions f() and g() legal C++ code? ...
> >
> > Are conversions done for the array size when calling new []?
>
> No. Section 5.3.4 "New" requires that the expression in
> brackets have integral type.
Hmmm... that seems arbitrarily restrictive to me. That means that the
value in the square brackets must either be a variable of type int, or
an integer constant, or something explicitly cast to int. Why can't
the compiler perform implicit casts or conversions? After all, it's
allowed to do so everywhere else!
--
Jim Hyslop
Spam can go here: postmaster@cyberpromo.com postmaster@savetrees.com
All great truths begin as heresies.
-- George Bernard Shaw.
Note to recruitment agencies: I am *NOT* interested in looking for a
new job. I will NOT pass your name on to other programmers because I
do not know you.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/06/13 Raw View
Tony Cook wrote:
>
> Steve Clamage (stephen.clamage@Eng.Sun.COM) wrote:
> : Tony Cook wrote:
> : >
> : > Are the functions f() and g() legal C++ code? ...
> : >
> : > Are conversions done for the array size when calling new []?
>
> : No. Section 5.3.4 "New" requires that the expression in
> : brackets have integral type.
>
> The same is true for the subscript expression itself:
>
> 5.2.1 Subscripting [expr.sub]
>
> 1 A postfix expression followed by an expression in square brackets is a
> postfix expression. One of the expressions shall have the type
> "pointer to T" and the other shall have enumeration or integral type.
> The result is an lvalue of type "T." The type "T" shall be a com-
> pletely-defined object type.3) The expression E1[E2] is identical (by
> definition) to *((E1)+(E2)). [Note: see _expr.unary_ and _expr.add_
> for details of * and + and _dcl.array_ for details of arrays. ]
>
> (except that it explicitly allows enumeration types too)
>
> Does this mean that a subscript expression doesn't allow conversions
> either?
Oops. I should have checked that section myself. Put it together
with the introductory matter in section 4 Conversions, and it
seems clear that you can use any value in brackets that can be
converted implicitly to an integral type. (I think the mention
of enumeration type above is redundant.)
So I think you should be able to write code like this:
struct X { operator int(); };
void foo(int x, double y, X z)
{
int *t1 = new int[x];
int *t2 = new int[y];
int *t3 = new int[z];
...
}
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jsa@edg.com (J. Stephen Adamczyk)
Date: 1997/06/15 Raw View
In article <33A0B615.154D@Eng.Sun.COM> Steve Clamage <stephen.clamage@eng.sun.com> writes:
>Tony Cook wrote:
>> : > Are conversions done for the array size when calling new []?
>>
>> : No. Section 5.3.4 "New" requires that the expression in
>> : brackets have integral type.
>>
>> The same is true for the subscript expression itself:
>> [... WP excerpt removed. ]
>> Does this mean that a subscript expression doesn't allow conversions
>> either?
>
>Oops. I should have checked that section myself. Put it together
>with the introductory matter in section 4 Conversions, and it
>seems clear that you can use any value in brackets that can be
>converted implicitly to an integral type. (I think the mention
>of enumeration type above is redundant.)
No, I'm afraid not. The subscript operator [] allows explicit conversions
because of the pseudo-prototype for operator[] provided in 13.6 [over.built]
paragraph 14. There is no similar mechanism for new[] and therefore
no implicit conversions are done there.
The WP used to say that conversions were done wherever necessary, but
that statement was removed in favor of being explicit about the places
where implicit conversions are allowed. There is no explicit statement
about implicit conversions for new[], so they aren't allowed.
Steve Adamczyk
Edison Design Group
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1997/06/09 Raw View
Are the functions f() and g() legal C++ code?
int *f(float i)
{
return new int[i];
}
class A
{
public:
operator int() const { return 5; }
};
int *g(A a)
{
return new int[a];
}
I get the following errors from one compiler:
newarrf.cpp(3): Error! E529: col(20) expression for number of array
elements must be integral
newarrf.cpp(14): Error! E529: col(20) expression for number of array
elements must be integral
Are conversions done for the array size when calling new []?
--
Tony Cook - tony@online.tmx.com.au <= testing .procmailrc
100237.3425@compuserve.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]