public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/43663] New: Can't take a const-ref to a bit field
@ 2010-04-06 10:49 gcc-bugzilla at daryl dot haresign dot com
2010-04-06 10:58 ` [Bug c++/43663] " rguenth at gcc dot gnu dot org
` (4 more replies)
0 siblings, 5 replies; 12+ messages in thread
From: gcc-bugzilla at daryl dot haresign dot com @ 2010-04-06 10:49 UTC (permalink / raw)
To: gcc-bugs
Given the code below, gcc emits the following error:
error: attempt to take address of bit-field structure member 'MyClass::field1'
This compiles fine with Sun's CC, IBM's xlC, and Comeau. The C++ 2003 standard
mentions in 9.6p2 that a non-const reference can't be bound to a bit field, but
that a const reference can be (although it's actually bound to a temporary
initialized with a copy of the bit field rather than the field itself) so I
expect the below code to work fine.
// Code
class MyClass
{
public:
MyClass() { field1 = 0; };
int field1 : 4;
};
int main()
{
MyClass myClass;
const int &fieldRef1 = myClass.field1;
return 0;
}
--
Summary: Can't take a const-ref to a bit field
Product: gcc
Version: 4.3.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: gcc-bugzilla at daryl dot haresign dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
2010-04-06 10:49 [Bug c++/43663] New: Can't take a const-ref to a bit field gcc-bugzilla at daryl dot haresign dot com
@ 2010-04-06 10:58 ` rguenth at gcc dot gnu dot org
2010-04-06 11:01 ` [Bug c++/43663] [C++0x] " steven at gcc dot gnu dot org
` (3 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2010-04-06 10:58 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from rguenth at gcc dot gnu dot org 2010-04-06 10:57 -------
Confirmed. Note that the frontend would need to make sure to unfold the
reference for the middle-end - the middle-end can't bear taking the address
of a bitfield.
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Ever Confirmed|0 |1
Keywords| |rejects-valid
Known to fail| |4.5.0
Last reconfirmed|0000-00-00 00:00:00 |2010-04-06 10:57:58
date| |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] [C++0x] Can't take a const-ref to a bit field
2010-04-06 10:49 [Bug c++/43663] New: Can't take a const-ref to a bit field gcc-bugzilla at daryl dot haresign dot com
2010-04-06 10:58 ` [Bug c++/43663] " rguenth at gcc dot gnu dot org
@ 2010-04-06 11:01 ` steven at gcc dot gnu dot org
2010-04-06 11:06 ` paolo dot carlini at oracle dot com
` (2 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: steven at gcc dot gnu dot org @ 2010-04-06 11:01 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from steven at gcc dot gnu dot org 2010-04-06 11:01 -------
Wow, taking the address of a bit field. That can only be C++. This should be
closed as a dup of bug 5... Oh, well... :-)
--
steven at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jason at gcc dot gnu dot org
Summary|Can't take a const-ref to a |[C++0x] Can't take a const-
|bit field |ref to a bit field
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] [C++0x] Can't take a const-ref to a bit field
2010-04-06 10:49 [Bug c++/43663] New: Can't take a const-ref to a bit field gcc-bugzilla at daryl dot haresign dot com
2010-04-06 10:58 ` [Bug c++/43663] " rguenth at gcc dot gnu dot org
2010-04-06 11:01 ` [Bug c++/43663] [C++0x] " steven at gcc dot gnu dot org
@ 2010-04-06 11:06 ` paolo dot carlini at oracle dot com
2010-04-06 11:38 ` [Bug c++/43663] " redi at gcc dot gnu dot org
2010-04-07 21:19 ` jason at gcc dot gnu dot org
4 siblings, 0 replies; 12+ messages in thread
From: paolo dot carlini at oracle dot com @ 2010-04-06 11:06 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from paolo dot carlini at oracle dot com 2010-04-06 11:05 -------
Why C++0x?!?
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
2010-04-06 10:49 [Bug c++/43663] New: Can't take a const-ref to a bit field gcc-bugzilla at daryl dot haresign dot com
` (2 preceding siblings ...)
2010-04-06 11:06 ` paolo dot carlini at oracle dot com
@ 2010-04-06 11:38 ` redi at gcc dot gnu dot org
2010-04-07 21:19 ` jason at gcc dot gnu dot org
4 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-04-06 11:38 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from redi at gcc dot gnu dot org 2010-04-06 11:38 -------
Noone's taking the address of a bitfield, the stnadard specifically says "The
address-of operator & shall not be applied to a bit-field, so there are no
pointers to bit-fields."
What happens is the same as:
double d = 0;
const int& i = d;
a temporary is created and the reference bound to it.
And this definitely isn't new in C++0x! The reference for C++03 should be 9.6p3
rather than p2
--
redi at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|[C++0x] Can't take a const- |Can't take a const-ref to a
|ref to a bit field |bit field
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
2010-04-06 10:49 [Bug c++/43663] New: Can't take a const-ref to a bit field gcc-bugzilla at daryl dot haresign dot com
` (3 preceding siblings ...)
2010-04-06 11:38 ` [Bug c++/43663] " redi at gcc dot gnu dot org
@ 2010-04-07 21:19 ` jason at gcc dot gnu dot org
4 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu dot org @ 2010-04-07 21:19 UTC (permalink / raw)
To: gcc-bugs
------- Comment #5 from jason at gcc dot gnu dot org 2010-04-07 21:19 -------
Long-standing bug.
--
jason at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to fail|4.5.0 |2.95.4 4.5.0
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
` (4 preceding siblings ...)
2012-10-11 2:35 ` paolo at gcc dot gnu.org
@ 2012-10-11 2:37 ` paolo.carlini at oracle dot com
5 siblings, 0 replies; 12+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-10-11 2:37 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Known to work| |4.7.0, 4.8.0
Resolution| |FIXED
--- Comment #11 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-10-11 02:37:29 UTC ---
Done.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
` (3 preceding siblings ...)
2012-10-11 1:42 ` paolo.carlini at oracle dot com
@ 2012-10-11 2:35 ` paolo at gcc dot gnu.org
2012-10-11 2:37 ` paolo.carlini at oracle dot com
5 siblings, 0 replies; 12+ messages in thread
From: paolo at gcc dot gnu.org @ 2012-10-11 2:35 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
--- Comment #10 from paolo at gcc dot gnu.org <paolo at gcc dot gnu.org> 2012-10-11 02:34:39 UTC ---
Author: paolo
Date: Thu Oct 11 02:34:32 2012
New Revision: 192347
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=192347
Log:
2012-10-10 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/43663
* g++.dg/init/bitfield3.C: New.
Added:
trunk/gcc/testsuite/g++.dg/init/bitfield3.C
Modified:
trunk/gcc/testsuite/ChangeLog
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
` (2 preceding siblings ...)
2011-05-15 12:54 ` james.dennett at gmail dot com
@ 2012-10-11 1:42 ` paolo.carlini at oracle dot com
2012-10-11 2:35 ` paolo at gcc dot gnu.org
2012-10-11 2:37 ` paolo.carlini at oracle dot com
5 siblings, 0 replies; 12+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-10-11 1:42 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
Paolo Carlini <paolo.carlini at oracle dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC|gcc-bugs at gcc dot gnu.org |
Target Milestone|--- |4.7.0
Known to fail| |
--- Comment #9 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-10-11 01:42:19 UTC ---
This is fixed in 4.7.0. I'm adding the testcase and closing the PR.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
2011-05-15 12:04 ` james.dennett at gmail dot com
2011-05-15 12:42 ` james.dennett at gmail dot com
@ 2011-05-15 12:54 ` james.dennett at gmail dot com
2012-10-11 1:42 ` paolo.carlini at oracle dot com
` (2 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: james.dennett at gmail dot com @ 2011-05-15 12:54 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
--- Comment #8 from James Dennett <james.dennett at gmail dot com> 2011-05-15 12:34:51 UTC ---
Interestingly this works with Apple's g++ 4.2.1, specifically
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3), but
not with their 4.0.1 release.
Tested with:
int main() {
struct S {
S(): i(0) {}
int i : 3;
};
S s;
printf("&s: %p\n", (void*)&s);
int const& cr(s.i); // should compile, binding to a temporary
printf("&cr: %p\n", (void*)&cr);
}
A non-const reference still correctly gives an error:
st.cc:12: error: invalid initialization of reference of type ‘int&’ from
expression of type ‘signed char:3’
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
2011-05-15 12:04 ` james.dennett at gmail dot com
@ 2011-05-15 12:42 ` james.dennett at gmail dot com
2011-05-15 12:54 ` james.dennett at gmail dot com
` (3 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: james.dennett at gmail dot com @ 2011-05-15 12:42 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
--- Comment #7 from James Dennett <james.dennett at gmail dot com> 2011-05-15 11:55:47 UTC ---
Unsurprisingly the quick hack isn't really good enough -- it'll happily bind a
non-const reference to a temporary initialized from a bitfield. (...and I
guess that's why we have tests, and code reviews.)
^ permalink raw reply [flat|nested] 12+ messages in thread
* [Bug c++/43663] Can't take a const-ref to a bit field
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
@ 2011-05-15 12:04 ` james.dennett at gmail dot com
2011-05-15 12:42 ` james.dennett at gmail dot com
` (4 subsequent siblings)
5 siblings, 0 replies; 12+ messages in thread
From: james.dennett at gmail dot com @ 2011-05-15 12:04 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43663
James Dennett <james.dennett at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |james.dennett at gmail dot
| |com
--- Comment #6 from James Dennett <james.dennett at gmail dot com> 2011-05-15 11:52:01 UTC ---
Here's a quick hack that causes a temporary to be generated when binding a
bit-field to a reference-to-const.
$ svn diff
Index: call.c
===================================================================
--- call.c (revision 173769)
+++ call.c (working copy)
@@ -8594,7 +8594,7 @@
expr = error_mark_node;
else
{
- if (!lvalue_or_rvalue_with_address_p (expr))
+ if (is_bitfield_expr_with_lowered_type (expr) ||
!lvalue_or_rvalue_with_address_p (expr))
{
tree init;
var = set_up_extended_ref_temp (decl, expr, cleanup, &init);
I'll try to make time to clean that up and add regression tests before running
it by someone with stronger gcc-fu.
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2012-10-11 2:37 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-06 10:49 [Bug c++/43663] New: Can't take a const-ref to a bit field gcc-bugzilla at daryl dot haresign dot com
2010-04-06 10:58 ` [Bug c++/43663] " rguenth at gcc dot gnu dot org
2010-04-06 11:01 ` [Bug c++/43663] [C++0x] " steven at gcc dot gnu dot org
2010-04-06 11:06 ` paolo dot carlini at oracle dot com
2010-04-06 11:38 ` [Bug c++/43663] " redi at gcc dot gnu dot org
2010-04-07 21:19 ` jason at gcc dot gnu dot org
[not found] <bug-43663-4@http.gcc.gnu.org/bugzilla/>
2011-05-15 12:04 ` james.dennett at gmail dot com
2011-05-15 12:42 ` james.dennett at gmail dot com
2011-05-15 12:54 ` james.dennett at gmail dot com
2012-10-11 1:42 ` paolo.carlini at oracle dot com
2012-10-11 2:35 ` paolo at gcc dot gnu.org
2012-10-11 2:37 ` paolo.carlini at oracle dot com
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).