Modify

Ticket #5279 (closed Patches: fixed)

Opened 3 years ago

Last modified 3 years ago

[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

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

Change History

Changed 3 years ago by mimomorin@…

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

comment:1 Changed 3 years ago by eric_niebler

  • Status changed from new to assigned
  • Milestone changed from To Be Determined to Boost 1.47.0

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 Changed 3 years ago by mimomorin@…

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 Changed 3 years ago by mimomorin@…

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.

Changed 3 years ago by mimomorin@…

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 Changed 3 years ago by eric_niebler

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

comment:5 Changed 3 years ago by eric_niebler

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

comment:6 Changed 3 years ago by eric_niebler

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 Changed 3 years ago by mimomorin@…

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 Changed 3 years ago by eric_niebler

  • Status changed from assigned to closed
  • Resolution set to fixed

merged to release. thanks for your help with this.

comment:9 Changed 3 years ago by pmachata@…

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.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.