https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61213

            Bug ID: 61213
           Summary: std::forward_as_tuple and std::tuple<T&&> and any
                    rvalue loose data
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tower120 at gmail dot com

std::forward_as_tuple elements loose data on -O2.
Works fine with -O0


Consider the following code:
http://coliru.stacked-crooked.com/a/67499e052283bd5a

#include <iostream>
#include <tuple>

class Widget {
public:
  Widget(int h) : h(h){}
  int h;
};

template<typename T>
struct RvalueTest{
    RvalueTest(T&& value) : value( std::forward<T>(value) ){}
    T&& value;
};

int main() {
    auto t = std::forward_as_tuple(Widget(12));
    //auto t = std::tuple<Widget&&>(Widget(12));  // this not work also
    std::cout << std::get<0>(t).h;                // <-- some random value here

    /// This one not work, either:    
    /*RvalueTest<Widget> r(Widget(12));
    std::cout << r.value.h;*/
}


Code from above works fine with clang compiler.
I suspect that rvalue object life time is wrong in -O2 mode.

Reply via email to