This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/56032] New: Uniform initialization of references


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56032

             Bug #: 56032
           Summary: Uniform initialization of references
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: xazax.hun@gmail.com


Consider the following code:

// ---- CODE ------

#include <iostream>
#include <vector>


class S {
public:
    S(const std::vector<char>& v_) : v{v_} {}
    void undefined() {
        std::cout << v.front() << std::endl;
    }
private:
    const std::vector<char>& v;
};

int main(){
    std::vector<char> foo {'f', 'a', 'i', 'l'};
    std::cout << foo.front() << std::endl;
    S s{foo};
    s.undefined();

    return 0;
}

// ---- END CODE ------

Compiled with: g++ -std=c++11 main.cpp

s.undefined(); prints invalid characters or crashes the executable.

I think the result of the problem is that, the: v{v_}
initialization creates a new temporary from the vector that is destroyed after
the execution leaves the scope of the constructor. ( This would only be the
intended behaviour in case v would be initialized from initializer list, but
{v_} is clearly not an initializer list here.)

If I replace the uniform initialization with regular one: v(v_)
the snippet above works as intended.

The very same snippet does not compile with gcc 4.5.2. Slightly related report
on that issue: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025.

I guess the origin of this problem is the incomplete fix of the error above.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]