1

The following code fragment does nothing, but illustrates the problem. It was extracted from some Boost Python code, which uses the Numpy C API. This was tested with the backport of a gcc 4.7 snapshot from Debian unstable to squeeze.

#include <boost/python/object.hpp>
#include <numpy/arrayobject.h>

int main(void)
{
  PyObject* obj=0;
  npy_int64 val;
  PyArray_ScalarAsCtype(obj, &val);
  return 0;
}

I'm compiling like this.

g++-4.7 -o warn.o -c -isystem /usr/include/python2.6 -fdiagnostics-show-option -ftemplate-depth-100 -fno-strict-aliasing -ansi -pedantic -Wextra -Wall -Werror -Wno-unused-function -Wc++0x-compat -g -O3 -std=c++11 -I/usr/include/python2.6 warn.cc
warn.cc: In function 'int main()':
warn.cc:8:3: error: ISO C++ forbids casting between pointer-to-function and pointer-to-object [-Werror]
cc1plus: all warnings being treated as errors

The problem is the -pedantic and the PyArray_ScalarAsCtype line of code. Without -pedantic the following compiles without error

g++-4.7 -o warn.o -c -isystem /usr/include/python2.6 -fdiagnostics-show-option -ftemplate-depth-100 -fno-strict-aliasing -ansi -Wextra -Wall -Werror -Wno-unused-function -Wc++0x-compat -g -O3 -std=c++11 -I/usr/include/python2.6 warn.cc
g++-4.7 -o warn warn.o -L/usr/lib/python2.6/config -lpython2.6 -lboost_python

Note: I added the =0 to suppress an uninitialized warning. Like I said, the code doesn't do anything.

I'd like to either suppress or remove the warning and keep the -pedantic flag. From what I've read, there is no error as such here, but this falls within some disputed section of the standard. I don't really understand the issue, or how it pertains to this line of code. The new gcc diagnostics allow one to selectively suppress warnings in a section of code, but they require you to know what specific flag is triggering the warning, and I don't know. Without the -Werror flag I get

warn.cc:8:3: warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object [enabled by default]
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83
  • The code is bad. Function pointers and object pointers aren't required to be the same size. A union should be used, or some other technique for ensuring that there's sufficient storage for either kind of pointer (and note that pointer-to-member may require even more storage, in the presence of virtual members and/or multiple virtual inheritance) – Ben Voigt Feb 17 '12 at 06:19
  • @BenVoigt: Unfortunately, I have no idea how to fix the issue. Any suggestions how implement a workaround, or even where to ask for a workaround? – Faheem Mitha Feb 17 '12 at 06:53

1 Answers1

1

In Standard C++, you cannot convert between, say, an int* and int(*)(). Likely, this is what's happening under the hood in your implementation. Most platforms allow it, but not all.

Of course, there is nothing illegal about any library only executing on platforms where it is legal.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Ok, can you suggest a workaround? I didn't write the Numpy function in question, and have no idea how to fix it. – Faheem Mitha Feb 17 '12 at 06:55
  • @FaheemMitha: It's possible that there is none, and the library only functions on platforms where this is legal. Certainly, I also didn't write it and have equally little idea what the problem is. – Puppy Feb 17 '12 at 07:03
  • Fair enough. How about simply suppressing the warning without altering the code, while retaining -pedantic? I could not find a way to do so, but it is possible there is some way. – Faheem Mitha Feb 17 '12 at 08:11
  • @Faheem: Ask the GCC documentation. – Puppy Feb 17 '12 at 10:33