public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/66093] New: g++ produces incorrect output on code with constexpr function initializing class with private fields
@ 2015-05-10 19:18 denvned at gmail dot com
  0 siblings, 0 replies; only message in thread
From: denvned at gmail dot com @ 2015-05-10 19:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 66093
           Summary: g++ produces incorrect output on code with constexpr
                    function initializing class with private fields
           Product: gcc
           Version: 5.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: denvned at gmail dot com
  Target Milestone: ---

Compiling the following code using command "g++ -std=c++14 main.cpp" produces
an incorrect result:

#include <iostream>

constexpr int n = 10;

struct A {
    constexpr operator const int*() const {
        return data;
    }

    constexpr operator int*() {
        return data;
    }

private:
    int data[n];
};

constexpr A f() {
    A a{};
    for (int i = 1; i <= n; i++) {
        a[i] = i;
    }
    return a;
}

A a = f();

int main()
{
    for (int i = 0; i < n; i++) {
        std::cout << a[i] << std::endl;
    }
}

Expected output of execution of this code is a list of numbers from 1 to 10.
But the program produced by GCC outputs a list of zeroes:
http://coliru.stacked-crooked.com/a/414c82777a938a80

The produced assembly code is clearly incorrect:
https://goo.gl/UJvcOJ

I have found that:
- If "private:" is removed, or
- if "constexpr" is removed from "f()", or
- if "A a = f();" is moved into the body of "main()", or
- if an user defined constructor initializing "data" is added,
then the bug disappears...

Clang produces correct output on this code.

I hope this information will help you fix the bug.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-05-10 19:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-10 19:18 [Bug c++/66093] New: g++ produces incorrect output on code with constexpr function initializing class with private fields denvned at gmail dot com

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