public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c++/9666: Arg conversion problem with operator new
@ 2003-02-14 21:22 bangerth
0 siblings, 0 replies; 2+ messages in thread
From: bangerth @ 2003-02-14 21:22 UTC (permalink / raw)
To: BrainChild, gcc-bugs, gcc-prs, nobody
Synopsis: Arg conversion problem with operator new
State-Changed-From-To: open->analyzed
State-Changed-By: bangerth
State-Changed-When: Fri Feb 14 21:22:28 2003
State-Changed-Why:
Confirmed -- well, sort of, since I am not sure whether this
is legal. Here's the code slightly reformatted:
-------------------------
#include <cstddef>
struct X {
operator size_t ();
operator size_t () const;
};
char *f2 (const X &x) { return new char[x]; }
--------------------------
The compiler complains:
tmp/g> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc
x.cc: In function `char* f2(const X&)':
x.cc:8: error: expression in new-declarator must have integral or enumeration
type
Now, I tend to think that the code is illegal, due to 5.3.4.7:
7 Every constant-expression in a direct-new-declarator shall be an inte-
gral constant expression (_expr.const_) and evaluate to a strictly
positive value. The expression in a direct-new-declarator shall have
integral type (_basic.fundamental_) with a non-negative value.
Then, on the other hand, this code compiles:
-----------------------
#include <cstddef>
struct X {
/* operator size_t (); */
operator size_t () const;
};
char *f2 (const X &x) { return new char[x]; }
-----------------------
I should think that the presence of the second non-const
operator shouldn't make a difference since x is a constant
reference...
Can someone clarify?
W.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9666
^ permalink raw reply [flat|nested] 2+ messages in thread
* c++/9666: Arg conversion problem with operator new
@ 2003-02-11 20:46 BrainChild
0 siblings, 0 replies; 2+ messages in thread
From: BrainChild @ 2003-02-11 20:46 UTC (permalink / raw)
To: gcc-gnats
>Number: 9666
>Category: c++
>Synopsis: Arg conversion problem with operator new
>Confidential: no
>Severity: non-critical
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Tue Feb 11 20:46:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator: Don Stauffer
>Release: 3.2
>Organization:
>Environment:
Cygwin under Windows 2000 Professional
>Description:
g++ produces error "size in array new must have integral type" if you use new[] with an argument of a class with two integral conversion operators which are identical exception one is a const method. The conversion works if used to pass the class object as a size_t function argument, and works with new if you remove either one of the conversion operators.
>How-To-Repeat:
#include <cstddef>
struct X
{
operator size_t () { return
2; }
operator size_t () const { return 3; }
};
char *f1 ( size_t n ) { return
new char[n]; }
char *f2 ( const X &x ) { return new
char[x]; }
int main (void) { }
/*
$ g t.cpp -ot
t.cpp: In function `char* f2(const X&)':
t.cpp:11: size in array new must have integral type
*/
>Fix:
Workaround: Call a function which converts the argument and allocates the memory rather than calling new directly.
>Release-Note:
>Audit-Trail:
>Unformatted:
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2003-02-14 21:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-14 21:22 c++/9666: Arg conversion problem with operator new bangerth
-- strict thread matches above, loose matches on Subject: below --
2003-02-11 20:46 BrainChild
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).