public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/63717] New: Value-initialization performs defaut-initialization when =default is outside the class declaration
@ 2014-11-02 22:50 morwenn29 at hotmail dot fr
  2014-11-03  0:58 ` [Bug c++/63717] " morwenn29 at hotmail dot fr
  2014-11-03  9:16 ` morwenn29 at hotmail dot fr
  0 siblings, 2 replies; 3+ messages in thread
From: morwenn29 at hotmail dot fr @ 2014-11-02 22:50 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 63717
           Summary: Value-initialization performs defaut-initialization
                    when =default is outside the class declaration
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: morwenn29 at hotmail dot fr

I have this minimal test case:

    struct foo
    {
        unsigned value;

        foo();
        foo(unsigned value):
            value(value)
        {}
    };

    foo::foo()
        = default;

    int main()
    {
        foo bar{};
    }

In this test case, foo bar{}; should zero-initialize bar and thus
zero-initialize value (see http://stackoverflow.com/q/26699720/1364752 for a
discussion about that). However, value is sometimes initialized with garbage
instead of being initialized with 0. Here is the assembly produced by the
example above:

    push   %ebp
    mov    %esp,%ebp
    and    $0xfffffff0,%esp
    sub    $0x10,%esp
    call   0x402060 <__main>
    lea    0xc(%esp),%eax
    mov    %eax,%ecx
    call   0x401610 <foo::foo()>
    mov    $0x0,%eax
    leave
    ret

As you can see, the default constructor of foo is called, but value is not
zero-initialized here. However, if we move = default in the class declaration:

    struct foo
    {
        unsigned value;

        foo() = default;
        foo(unsigned value):
            value(value)
        {}
    };

    foo::foo()
        = default;

    int main()
    {
        foo bar{};
    }

Then, the produced assembly is not the same anymore and value is properly
zero-initialized as expected:

    push   %ebp
    mov    %esp,%ebp
    and    $0xfffffff0,%esp
    sub    $0x10,%esp
    call   0x402050 <__main>
    movl   $0x0,0xc(%esp) ; init value (?)
    mov    $0x0,%eax
    leave
    ret

I am not really good at reading assembly but I fixed my problem by moving the
=default into the class instead separating the declaration of the default
constructor and its definition. If I correctly read both the standard and the
assembly, I am pretty sure that this is a GCC bug.


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

* [Bug c++/63717] Value-initialization performs defaut-initialization when =default is outside the class declaration
  2014-11-02 22:50 [Bug c++/63717] New: Value-initialization performs defaut-initialization when =default is outside the class declaration morwenn29 at hotmail dot fr
@ 2014-11-03  0:58 ` morwenn29 at hotmail dot fr
  2014-11-03  9:16 ` morwenn29 at hotmail dot fr
  1 sibling, 0 replies; 3+ messages in thread
From: morwenn29 at hotmail dot fr @ 2014-11-03  0:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Morwenn <morwenn29 at hotmail dot fr> ---
I forgot to remove the outside-class =default in the second example, it should
be:

    struct foo
    {
        unsigned value;

        foo() = default;
        foo(unsigned value):
            value(value)
        {}
    };

    int main()
    {
        foo bar{};
    }

Sorry for that.


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

* [Bug c++/63717] Value-initialization performs defaut-initialization when =default is outside the class declaration
  2014-11-02 22:50 [Bug c++/63717] New: Value-initialization performs defaut-initialization when =default is outside the class declaration morwenn29 at hotmail dot fr
  2014-11-03  0:58 ` [Bug c++/63717] " morwenn29 at hotmail dot fr
@ 2014-11-03  9:16 ` morwenn29 at hotmail dot fr
  1 sibling, 0 replies; 3+ messages in thread
From: morwenn29 at hotmail dot fr @ 2014-11-03  9:16 UTC (permalink / raw)
  To: gcc-bugs

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

Morwenn <morwenn29 at hotmail dot fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Morwenn <morwenn29 at hotmail dot fr> ---
Not a bug actually, sorry for that (that makes two "sorry" in two messages). I
read again [dcl.fct.def.default] and it appears that I was wrong; I totally
missed the "first declaration" part in the following statement:

A function is user-provided if it is user-declared and not explicitly defaulted
or
deleted on its first declaration.


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

end of thread, other threads:[~2014-11-03  9:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-02 22:50 [Bug c++/63717] New: Value-initialization performs defaut-initialization when =default is outside the class declaration morwenn29 at hotmail dot fr
2014-11-03  0:58 ` [Bug c++/63717] " morwenn29 at hotmail dot fr
2014-11-03  9:16 ` morwenn29 at hotmail dot fr

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).