dimanche 14 octobre 2018

Declare C++ array type with dynamic size works fine in Linux, but not Windows

With recent gcc versions, I can declare dynamic-sized array data types like this:

#include <stdio.h>

int main() {
  int u;
  scanf("%d", &u);

  using my_type = int[u];
  printf("size of my_type: %d\n", sizeof(my_type));

  return 0;
}

This compiles with gcc successfully, and works fine in Linux. Enter 5 for example as input, this will print 20.

But with Windows compiler cl.exe or VS2015 itself, I receive compile error:

error C2540: non-constant expression as array bound

How can I solve this?

Aucun commentaire:

Enregistrer un commentaire