Where's g++'s VLA extension?

六月ゝ 毕业季﹏ 提交于 2019-12-12 05:38:20

问题


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")];
}


来源:https://stackoverflow.com/questions/4151254/wheres-gs-vla-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!