Opened 12 years ago

Closed 12 years ago

#5190 closed Bugs (fixed)

boost::phoenix operator+ sometimes returns a reference to a temporary leading to undefined behaviour

Reported by: nicos@… Owned by: Joel de Guzman
Milestone: To Be Determined Component: phoenix
Version: Boost 1.45.0 Severity: Showstopper
Keywords: Cc:

Description

Compiling:

const float x = 1.0f;
const float y = 1.0f;
std::cout << (arg1+arg2)(x,y);

with gcc results in the following warning: /usr/local/boost_1_45_0/boost/spirit/home/phoenix/operator/arithmetic.hpp:74: warning: returning reference to temporary

The trouble is that result_of_plus<const float, const float>::type is const float& instead of float. The source of the issue is that float is not supported "natively" by phoenix type deduction system ( if float is replaced by int, then the return type is correctly deduced ), the selected test overload is:

    template <typename X, typename Y>
    typename disable_if<
        is_basic<X>
      , x_reference_type
    >::type
    test(X&);

As a result a reference to a temporary is returned that leads to undefined behaviour in programs compiled with heavy optimizations (-O3).

Change History (1)

comment:1 by Thomas Heller, 12 years ago

Component: spiritphoenix
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.