On 05-02-14 21:40, Eric Blake wrote:
> Actually, in gcc's case, your proposed program is now a syntax error:
>
> $ gcc -o foo foo.c
> foo.c: In function ‘main’:
> foo.c:1:21: error: ‘new’ undeclared (first use in this function)
>  int main() { (void) new int; return 0; }
>                      ^
> foo.c:1:21: note: each undeclared identifier is reported only once for
> each function it appears in
> foo.c:1:25: error: expected ‘;’ before ‘int’
>  int main() { (void) new int; return 0; }
>
> But that's equally useful for weeding out invalid C++ compilers :)

That's because you named the file "foo.c".

$ gcc -o foo foo.cpp
/tmp/ccVVxj3a.o: In function `main':
foo.cpp:(.text+0xa): undefined reference to `operator new(unsigned long)'
collect2: error: ld returned 1 exit status

> I'm wondering if we should avoid the memory leak by using 'delete new
> int' instead of '(void) new int', just so we are less likely to trip up
> on a compiler warning causing a false negative.  Or maybe even some
> other construct that doesn't involve memory allocation but is truly a
> no-op C++ program that fails to compile under C (such as
> AC_LANG_PROGRAM([class foo]) by exploiting 'class' rather than 'new').
> Any opinions?  Otherwise, the idea for your patch looks good to me.

'delete new int' should work as well. But I don't think defining a class
is sufficient. To catch GCC, you need something that will cause a link
error. Something like this would work:

#include <string>
int main() { std::string s; return 0; }

But using the standard library is difficult if you want to support old
compilers that don't have namespace support.



Reply via email to