简体   繁体   English

为什么没有clang -Wunused-member-function警告未使用的成员函数?

[英]Why doesn't clang -Wunused-member-function warn about an unused member function?

I'm compiling a very basic program attempting to trigger output from -Wunused-member-function . 我正在编译一个非常基本的程序,试图从-Wunused-member-function触发输出。

test.cpp: TEST.CPP:

#include <iostream>

class A {
    public:
    void foo() { std::cout << "Called foo" << std::endl; }
    void foo_unused() { std::cout << "Unused foo" << std::endl; }
};

int main() {
    A obj;
    obj.foo();
    return 0;
}

The output from the following command 以下命令的输出

clang++ -std=c++17 -Wall -Wunused -Wunused-member-function \
        -Wunused-function -Wunneeded-member-function \
        test.cpp -o test

unfortunately contained not even a single warning. 不幸的是,甚至没有一个警告。 I expected the compiler to warn about foo_unused not being used. 我希望编译器警告foo_unused没有被使用。

Is there a different behavior I'm missing here? 我在这里缺少不同的行为吗? Otherwise, why doesn't clang complain about the unused member function? 否则,为什么不抱怨未使用的成员函数?

You have to put the class in an anonymous namespace to make the warning appear: 您必须将该类放在匿名命名空间中才能显示警告:

namespace
{
class A {
    public:
    void foo() { std::cout << "Called foo" << std::endl; }
    void foo_unused() { std::cout << "Unused foo" << std::endl; }
};
}

You can see that now you have the warning: https://godbolt.org/z/15Buo- 您现在可以看到警告: https//godbolt.org/z/15Buo-

The reason is that this version can activate clang checks on public methods as well, as you explicitely said that this class will not be accessible in another translation unit. 原因是此版本也可以激活对公共方法的clang检查,因为您明确表示此类将无法在另一个翻译单元中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM