https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96507

            Bug ID: 96507
           Summary: Feature request: improve "-Waddress" (or equivalent)
                    for function references inside structs
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.kircheis at gmail dot com
  Target Milestone: ---

This is a feature request.


I'm compiling the code with "-Wall -Wextra -pedantic"

----
#include <utility>

void foo(){}

int main(){
    void(&f)() = foo;
    static_assert(!std::is_pointer<decltype(f)>::value, "should not be a
pointer");
    return f != 0;
}
----

gcc correctly triggers the warning "warning: the address of 'void foo()' will
never be NULL [-Waddress]" (nitpick nullptr in the error message would be
better than NULL).

Also for following example gcc emits the desired diagnostic

----
#include <utility>

void foo(){}

using fun = void();

fun& get_fun();

int main(){
    fun& f = get_fun();
    static_assert(!std::is_pointer<decltype(f)>::value, "should not be a
pointer");
    return f != 0;
}
----

But if the function reference is inside a struct:

----
#include <utility>

void foo(){}

using fun = void();

struct s{
    fun& f;
};

s get_fun();

int main(){
    s ss = get_fun();
    static_assert(!std::is_pointer<decltype(ss.f)>::value, "should not be a
pointer");
    return ss.f != 0;
}
----


then gcc does not emit any diagnostic (notice: it does if it can inline
get_fun()), even if "knows" that it cannot be null, as the static_assert does
not fire.

As reference: https://godbolt.org/z/vqohqM

Reply via email to