#5279 closed Patches (fixed)
[Foreach] Compile-time const rvalue detection fails with gcc 4.6
Reported by: | Owned by: | Eric Niebler | |
---|---|---|---|
Milestone: | Boost 1.47.0 | Component: | foreach |
Version: | Boost Development Trunk | Severity: | Problem |
Keywords: | Cc: |
Description
Recently, gcc 4.6 changed the behavior of rvalue conversions (from gcc-4.6-20110305). You can see the related info here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47851
This breaks compile-time const rvalue detection in Boost.Foreach; Const rvalues are incorrectly treated as lvalues, and so a segmentation fault occurs in the following code:
#include <list> #include <boost/foreach.hpp> typedef const std::list<int> clist; int main (int argc, char* argv[]) { BOOST_FOREACH(int x, clist(3)) {} return 0; }
Though gcc 4.6 is not yet released, I think this behavior is highly likely to happen in the release version. So I reported this problem here.
Attachments (2)
Change History (11)
by , 12 years ago
Attachment: | foreach.patch added |
---|
comment:1 by , 12 years ago
Milestone: | To Be Determined → Boost 1.47.0 |
---|---|
Status: | new → assigned |
I wonder if there is a different hack for gcc 4.6 that lets us detect const-qualified rvalues at compile time. gcc 4.6 supports rvalue references, right? It seems any compiler that supports rvalue references can detect rvalues at compile time. A general fix would be VERY GOOD.
comment:2 by , 12 years ago
Yeah, with the C++0x features (-std=c++0x
), we can easily detect rvalueness.
Personally, I use the following code for rvalue detection
#ifndef BOOST_NO_DECLTYPE #define BOOST_IS_LVALUE(xxx) boost::is_lvalue_reference<decltype( (xxx) )>::value #define BOOST_IS_RVALUE(xxx) (!BOOST_IS_LVALUE(xxx)) #endif
Is it better to develop a method that only needs rvalue reference support (i.e. without using the decltype feature)?
comment:3 by , 12 years ago
I will attach a patch which only needs rvalue reference support.
I ran the tests locally using gcc 4.6 (w/wo -std=c++0x
), and all the tests passed.
by , 12 years ago
Attachment: | foreach.2.patch added |
---|
A patch against trunk. Disables compile-time const rvalue detection in gcc 4.6 (and newer versions) and enables it when a compiler supports rvalue references.
comment:5 by , 12 years ago
Hrm, for me this fails with gcc 4.5.0 with -std=gnu++0x. Can you confirm?
comment:6 by , 12 years ago
Disregard. There seems to be something wrong with my setup that makes it impossible for me to test with gcc in 0x mode. I'll commit this (modulo some minor changes) and hope for the best.
comment:7 by , 12 years ago
Thanks, Eric! The regression test results seem OK.
I've also tried to support rvalue reference binding of temporary collections in Boost.Foreach
(auto
or decltype
C++0x feature is needed to extend the lifetime of temporaries).
A new ticket and patches for this coming soon.
comment:8 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
merged to release. thanks for your help with this.
comment:9 by , 11 years ago
Hi there. So for GCC 4.6, foreach will use BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION, which implies that BOOST_FOREACH cannot be used to iterate over noncopyable collections, as noted in the file. Is there a plan to fix this? I'm currently hitting this case when building Wesnoth. Thanks, Petr Machata.
Disables compile-time const rvalue detection in gcc 4.6 (and newer versions)