http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53166

             Bug #: 53166
           Summary: static_assert produces bogus warning
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: andyg1...@hotmail.co.uk


The following code produces a bogus compilation warning:


template <typename X, X a>
struct A {
    static_assert(a != nullptr, "oops");
};

int a1;
A<int*, &a1> a2;     // this produces a bogus warning
A<int*, nullptr> a3; // correctly generates static assertion


The bogus warning is for the declaration of "a2" saying "the address of ‘a1’
will never be NULL".  This is, of course, true but is exactly what the
static_assert is intended to ensure! (cf. the declaration of "a3").  Here is
the output from gcc:

$ gcc-4.7 -c --std=c++11 bugreport.cpp -Wall -Werror
bugreport.cpp: In instantiation of ‘struct A<int*, (& a1)>’:
bugreport.cpp:7:14:   required from here
bugreport.cpp:2:10: error: the address of ‘a1’ will never be NULL
[-Werror=address]
bugreport.cpp: In instantiation of ‘struct A<int*, 0u>’:
bugreport.cpp:8:18:   required from here
bugreport.cpp:3:5: error: static assertion failed: oops
cc1plus: all warnings being treated as errors

My expectation would be that the static_assert should silently pass, not cause
a warning in this case.  Compare the output that clang gives:

bugreport.cpp:3:5: error: static_assert failed "oops"
    static_assert(a != nullptr, "oops");
    ^             ~~~~~~~~~~~~
bugreport.cpp:8:18: note: in instantiation of template class 'A<int *,
nullptr>' requested here                                     
A<int*, nullptr> a3; // correctly generates static assertion
                 ^
1 error generated.

Reply via email to