Reputation: 784
In my C++ program at some point I do:
std::cin >> my_int;
my_class my_array[my_int];
When I compile it with g++
on OSX, I get:
error: variable length array of non-POD element type
as expected. However, when I compile it on Ubuntu, I do not get any errors. In both scenarios I compile with no options.
For reference, g++ --version
on OSX outputs:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
while on Ubuntu it outputs:
g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
Any ideas on why I do not get the error?
P.S.: I tried apt-get update
and apt-get install g++
but for some reason I get as a response that I have the latest version, which if I am not mistaken is 4.9.0, which I do not have...
Upvotes: 2
Views: 668
Reputation: 263507
Any conforming ISO C++ compiler is required to issue a diagnostic for that error. More precisely, it's required to issue at least one diagnostic message for any program that violates certain rules given in the ISO standard.
g++
is not a fully conforming C++ compiler by default.
To make it (attempt to) conform to the C++ standard, use the -pedantic
option, preferably along with an option to specify an edition of the C++ standard.
For example, when I compile something similar to your program, g++ does not give any warnings by default -- but when I compile it with
g++ -pedantic
or
g++ -std=c++11 -pedantic`
I get:
c.cpp: In function ‘int main()’:
c.cpp:5:24: warning: ISO C++ forbids variable length array ‘my_array’ [-Wvla]
For more information on g++'s conformance to the C++ standard(s), see the manual; type info g++
and read the "Standards" and "C++ Extensions" sections (links are to an online version of the manual).
Upvotes: 3
Reputation: 12425
GCC supports some non-standard extensions, one of which is variable length arrays of certain non-POD types.
Clang on the other hand, strives to be much more standards compliant.
From the Clang compatibility FAQ:
GCC and C99 allow an array's size to be determined at run time. This extension is not permitted in standard C++. However, Clang supports such variable length arrays in very limited circumstances for compatibility with GNU C and C99 programs:
- The element type of a variable length array must be a POD ("plain old data") type, which means that it cannot have any user-declared constructors or destructors, any base classes, or any members of non-POD type. All C types are POD types.
- Variable length arrays cannot be used as the type of a non-type template parameter.
As @Brian pointed out, you're using Clang on your OSX machine (no surprise, Apple created LLVM) and GCC on your Ubuntu box.
Upvotes: 0