g++ -Waddress may misunderstand my meaning

两盒软妹~` 提交于 2019-12-10 10:57:38

问题


The example code is:

#include <iostream>

using std::cout;
using std::endl;

void bar(double *) {
    cout << "call bar()" << endl;
}

using Bar = void(*)(double *);

template <Bar pfunction>
void foo() {
    // when call "foo<bar>()", there is a warning:
    // the address of ‘void bar(double*)’ will never be NULL [-Waddress]
    if (nullptr != pfunction) {
        pfunction(nullptr);
    }
    cout << "shit" << endl;
}

int main() {
    foo<nullptr>(); // OK
    foo<bar>(); // warning

    return 0;
}

from gcc manual:

-Waddress

Warn about suspicious uses of memory addresses. These include using the address of a function in a conditional expression, such as "void func(void); if (func)", and comparisons against the memory address of a string literal, such as "if (x == "abc")". Such uses typically indicate a programmer error: the address of a function always evaluates to true, so their use in a conditional usually indicate that the programmer forgot the parentheses in a function call.

The last sentence misunderstands my meaning. As in the code, it's necessary to test whether the function pointer is nullptr. Should I add -Wno-address or modify my code to silence the warning?

Update on 2015.9.15. As @SergeyA says, I use template specialization, and all work well.

template <Bar pfunction>
void foo() {
    pfunction(nullptr);
    cout << "shit" << endl;
}

template <>
void foo<nullptr>() {
    cout << "shit" << endl;
}

回答1:


Because this is compile-time code, in this template instantiation Bar will never be null - there is only one value for this. You should not mix compile-time programming with dynamic branching. To achieve your goal (not that I understand why you want your Bar to be template argument) you need to have a specialization of your foo for nullptr.



来源:https://stackoverflow.com/questions/32166386/g-waddress-may-misunderstand-my-meaning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!