Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#5279 closed Patches (fixed)

[Foreach] Compile-time const rvalue detection fails with gcc 4.6

Reported by: mimomorin@… 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)

foreach.patch (953 bytes ) - added by mimomorin@… 12 years ago.
Disables compile-time const rvalue detection in gcc 4.6 (and newer versions)
foreach.2.patch (2.0 KB ) - added by mimomorin@… 12 years ago.
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.

Download all attachments as: .zip

Change History (11)

by mimomorin@…, 12 years ago

Attachment: foreach.patch added

Disables compile-time const rvalue detection in gcc 4.6 (and newer versions)

comment:1 by Eric Niebler, 12 years ago

Milestone: To Be DeterminedBoost 1.47.0
Status: newassigned

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 mimomorin@…, 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 mimomorin@…, 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 mimomorin@…, 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:4 by Eric Niebler, 12 years ago

Thanks for the patch. I hope to get to this soon.

comment:5 by Eric Niebler, 12 years ago

Hrm, for me this fails with gcc 4.5.0 with -std=gnu++0x. Can you confirm?

comment:6 by Eric Niebler, 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 mimomorin@…, 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 Eric Niebler, 12 years ago

Resolution: fixed
Status: assignedclosed

merged to release. thanks for your help with this.

comment:9 by pmachata@…, 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.

Note: See TracTickets for help on using tickets.