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 via email to