Where's g++'s VLA extension?

2019-08-18 03:00发布

问题:

My question is related to this thread.

Here is the code

#include <stdio.h>

int main(int argc, char *argv[printf("Hello, world!\n")]) {}

I accidently saved it as a *.cpp file and tried to compile it with g++. But I got an error and a warning.

error: expected ',' or '...' before 'argv'
warning: second argument of 'int main(int, char*)' should be 'char ** '

I know the above code is not Standard C++ [size of an array must be a constant expression in C++] but I always thought g++ supports Varible Length Array as an extension. Where am I wrong?

P.S : The above code gets compiled with CLang++

C:\Users\SUPER USER\Desktop>type check.cpp
#include <stdio.h>

int main(int argc, char *argv[printf("Hello, world!\n")]) {}
C:\Users\SUPER USER\Desktop>clang++ check.cpp

C:\Users\SUPER USER\Desktop>

回答1:

g++ allows (again, as an extension) VLAs. I think it just doesn't allow them in parameter lists. This compiles in g++ 4.4.1.

#include <stdio.h>

int main(int argc, char *argv[])
{
    char *array[printf("Hello, world!\n")];
}


标签: c++ g++