public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "roger.ferrer at bsc dot es" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/64002] New: Braced initialization of unknown bound array of nondependent type
Date: Thu, 20 Nov 2014 17:39:00 -0000	[thread overview]
Message-ID: <bug-64002-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 64002
           Summary: Braced initialization of unknown bound array of
                    nondependent type
           Product: gcc
           Version: 4.9.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: roger.ferrer at bsc dot es

Hi,

during an initialization of an array of unknown bound of
non-dependent type using a braced initializer which contains
type-dependent expressions, g++ does not "postpone" the computation
of the array size at instantiation time and seems to use the number
of elements inside the braced-initializer.

In all cases the code compiles fine.

  #include <cassert>

  struct A
  {
    int x, y;
  };

  template <typename T>
  int f1(T t1, T t2)
  {
    A a[] = { t1, t2 };
    return sizeof(a) / sizeof(a[0]);
  }

  void test1()
  {
    assert(f1(A(), A()) == 2); // OK
    assert(f1(1, 2) == 1); // ERROR: this assert fails at runtime
  }

That said, in the context of C++2011 a parameter pack expansion seems to work
correctly.

  template <typename ...T>
  int f2(T ...n)
  {
    A a[] = { n... };
    return sizeof(a) / sizeof(a[0]);
  }

  void test2()
  {
    assert(f2(A(), A()) == 2); // OK
    assert(f2(1, 2) == 1); // OK
  }

All asserts pass correctly both in clang-3.5 and icc 14.0.2

Kind regards,


             reply	other threads:[~2014-11-20 17:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-20 17:39 roger.ferrer at bsc dot es [this message]
2014-11-20 17:41 ` [Bug c++/64002] " roger.ferrer at bsc dot es
2014-12-10 20:38 ` ville.voutilainen at gmail dot com
2015-08-13 10:51 ` paolo.carlini at oracle dot com
2023-12-11 17:50 ` ppalka at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-64002-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).