public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/56032] New: Uniform initialization of references
@ 2013-01-18 14:17 xazax.hun at gmail dot com
  2013-01-18 15:02 ` [Bug c++/56032] " xazax.hun at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: xazax.hun at gmail dot com @ 2013-01-18 14:17 UTC (permalink / raw)
  To: gcc-bugs


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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/56032] Uniform initialization of references
  2013-01-18 14:17 [Bug c++/56032] New: Uniform initialization of references xazax.hun at gmail dot com
@ 2013-01-18 15:02 ` xazax.hun at gmail dot com
  2013-01-18 15:35 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: xazax.hun at gmail dot com @ 2013-01-18 15:02 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #1 from Gábor Horváth <xazax.hun at gmail dot com> 2013-01-18 15:01:45 UTC ---
(In reply to comment #0)
> 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.

- I think the result of the problem is that, the: v{v_}
+ I think the source of the problem is that, the: v{v_}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/56032] Uniform initialization of references
  2013-01-18 14:17 [Bug c++/56032] New: Uniform initialization of references xazax.hun at gmail dot com
  2013-01-18 15:02 ` [Bug c++/56032] " xazax.hun at gmail dot com
@ 2013-01-18 15:35 ` redi at gcc dot gnu.org
  2013-01-18 15:39 ` xazax.hun at gmail dot com
  2013-01-18 16:00 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2013-01-18 15:35 UTC (permalink / raw)
  To: gcc-bugs


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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE
           Severity|major                       |normal

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-18 15:35:19 UTC ---
(In reply to comment #0)
> I guess the origin of this problem is the incomplete fix of the error above.

There is no fix, PR 50025 is still open and this is just a dup of it.

N.B. the C++11 standard actually required this behaviour, but that's a defect
that's been fixed in the latest draft.

*** This bug has been marked as a duplicate of bug 50025 ***


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/56032] Uniform initialization of references
  2013-01-18 14:17 [Bug c++/56032] New: Uniform initialization of references xazax.hun at gmail dot com
  2013-01-18 15:02 ` [Bug c++/56032] " xazax.hun at gmail dot com
  2013-01-18 15:35 ` redi at gcc dot gnu.org
@ 2013-01-18 15:39 ` xazax.hun at gmail dot com
  2013-01-18 16:00 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: xazax.hun at gmail dot com @ 2013-01-18 15:39 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #3 from Gábor Horváth <xazax.hun at gmail dot com> 2013-01-18 15:39:08 UTC ---
(In reply to comment #2)
> (In reply to comment #0)
> > I guess the origin of this problem is the incomplete fix of the error above.
> 
> There is no fix, PR 50025 is still open and this is just a dup of it.

I added a new report because 50025 is about a compilation error, and with gcc
4.7 the code compiles, but fails to work as expected, but probably there issues
are closely related.

>
> N.B. the C++11 standard actually required this behaviour, but that's a defect
> that's been fixed in the latest draft.
> 

Could you point me to which part of the draft required this behaviour?

Thanks for your response,
Gábor


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/56032] Uniform initialization of references
  2013-01-18 14:17 [Bug c++/56032] New: Uniform initialization of references xazax.hun at gmail dot com
                   ` (2 preceding siblings ...)
  2013-01-18 15:39 ` xazax.hun at gmail dot com
@ 2013-01-18 16:00 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2013-01-18 16:00 UTC (permalink / raw)
  To: gcc-bugs


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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-18 16:00:15 UTC ---
(In reply to comment #3)
> I added a new report because 50025 is about a compilation error, and with gcc
> 4.7 the code compiles, but fails to work as expected, but probably there issues
> are closely related.

The difference is not caused by GCC 4.7, it's because you used a
const-reference and PR 50025 uses a non-const reference. If you change your
code to use non-cosnt references you get a compilation error too.

> Could you point me to which part of the draft required this behaviour?

See the comments for PR 50025, there's a link to DR 1288


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-01-18 16:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-18 14:17 [Bug c++/56032] New: Uniform initialization of references xazax.hun at gmail dot com
2013-01-18 15:02 ` [Bug c++/56032] " xazax.hun at gmail dot com
2013-01-18 15:35 ` redi at gcc dot gnu.org
2013-01-18 15:39 ` xazax.hun at gmail dot com
2013-01-18 16:00 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).