bogus warning for constexpr if with function pointer template parameter

Viewed 81

I always get a mysterious warning if I compile the following code:

template < void(*FUNC)() >
struct Bla
{
    static void Do()
    {
        if constexpr ( FUNC != nullptr )// << compiler complains here "the address of 'void f1()' will never be NULL [-Waddress]"
        {
            FUNC();
        }
        else
        {
            std::cout << "Nothing" << std::endl;
        }
    }
};

void f1() { std::cout << "f1" << std::endl; }

int main()
{
    Bla<f1>::Do();
    Bla<nullptr>::Do();
}

Full warning from g++:

[x@y ~]$ g++ -std=c++20 -g -O2 -Wall -pedantic -Wextra main.cpp
main.cpp: In instantiation of 'static void Bla<FUNC>::Do() [with void (* FUNC)() = f1]':
main.cpp:86:18:   required from here
main.cpp:71:13: warning: the address of 'void f1()' will never be NULL [-Waddress]
   71 |             if constexpr ( FUNC != nullptr )
      |             ^~

The idea of checking for template parms with constexpr if is not to get a warning if the parameter is a constant :-)

I am wrong or is it simply a gcc bug?

0 Answers
Related