It's gratifying when the compiler is right

I've been playing with clang because I'd like to use its static analyzer on the code at $DAY_JOB. Unfortunately, it frequently bombs while building our code. Fortunately, many of the bombs are due to our bugs, not theirs. Here's one example:

The following code builds with g++ 4.4.3 (Ubuntu 4.4.3-4ubuntu5) but not with clang version 3.1 (trunk 147292):

template<class Predicate>
void find_if(Predicate p) {
    void *x = 1;
}

g++ "mostly ignores" the body of find_if since it's never instantiated, while clang determines that you "cannot initialize a variable of type 'void *' with an rvalue of type 'int'".

Thanks, clang!

Entry first conceived on 27 December 2011, 20:35 UTC, last modified on 9 April 2012, 20:16 UTC
Website Copyright © 2004-2024 Jeff Epler