Search code examples
c++osx-snow-leopardllvm-gcc

llvm-g++-4.2 and C++ new-style type casting


I'm facing a weird problem. I looked around all over stackoverflow.com and elsewhere, but I didn't find an answer.

Here's some background: I'm writing a simple library; mostly for educational purposes and to understand C++ better. I'm using a MacBook Pro with Snow Leopard. I have an installation of Xcode 4.2 as well, however I use only VIM to edit my code. I recently finished implementing the GNU build system for whatever I have so far, so that I do not have to depend on Xcode anymore, and at the same time not have to write and maintain custom Makefiles. I'm using highest levels of warnings. Here're the flags I've got so far (but I keep adding on to them incrementally, understanding the reasons for each flag as I go): -Wall -Wmissing-field-initializers -Wno-trigraphs -Wreturn-type -Wnon-virtual-dtor -Woverloaded-virtual -Wmissing-braces -Wparentheses -Wswitch -Wunused-value -Wsign-compare -Waddress -Wsequence-point -Wshorten-64-to-32 -Wwrite-strings -Wold-style-cast, and finally, -Werror.

Now, I have a function which very simply returns a value:

wint_t weof() {
  return static_cast<wint_t>(WEOF);
}

However, when I compile this code, I get the warning "use of old-style cast". I don't understand how static_cast<> can be "old-style". Could anybody explain to me why this is happening? I thought I'd find the answer by looking at the definition of WEOF, but on Mac it's defined to __DARWIN_WEOF and I can't seem to find the definition of that.

Thanks!


Solution

  • Most likely the __DARWIN_WEOF macro itself is using an old-style cast (doing some web searching, it seems the value is likely ((__darwin_wint_t)-1)). If you are using C-compatible headers, it's unlikely you'll be able to fully enable -Wold-style-cast.

    As a workaround, you could try surrounding any offending code with an appropriate #pragma to disable this warning.

    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wold-style-cast"
    // ... code goes here ...
    #pragma GCC diagnostic pop
    

    But this will probably get pretty tiresome/verbose after a while...