gpt4 book ai didi

C++ 静态分析,模板类

转载 作者:行者123 更新时间:2023-11-30 05:10:38 28 4
gpt4 key购买 nike

我需要一个静态分析器来找到未初始化的变量成员/变量...模板化类类型。

任何分析器都可以做到这一点吗?我尝试了 clang/cppcheck 和其他几个,但没有成功。

这是我的测试代码:

enum class ViewMode
{
One = 1,
Two = 2,
Three = 3,
Four = 4
};

class TestClass {
public:
TestClass() {}
};

template<typename T, bool C = std::is_copy_constructible<T>::value>
class TemplateTest
{
public:

TemplateTest() {}

TemplateTest(const T& value)
: value_(value)
{}

TemplateTest(const TemplateTest&) = delete;

TemplateTest(TemplateTest<T, C>&& rhs)
: value_(std::move(rhs.value_))
{}

TemplateTest(T&& value)
: value_(std::move(value))
{}
private:
T value_;

};

class StaticAnalysisTest {

public:
StaticAnalysisTest() {}

void DoSomething() {

}

private:
ViewMode viewMode_; //this uninitialized warning is found
TemplateTest<ViewMode> viewMode2_; //this one is not
};

我将问题进一步提炼为:

class Foo
{
private:
int m_nValue;
public:
Foo() {};
Foo(int value) : m_nValue(value) {}
int GetValue() { return m_nValue; }
};

class Bar
{
public:
Bar(){}

void DoSomething() {
Foo foo;
}
};

这不会生成单元化变量警告,但是当我注释掉时:

 //Foo(int value) : m_nValue(value) {}

确实如此

最佳答案

感谢您评估 Cppcheck。如果您添加 --inconclusive 标志,则第二个示例会发出警告,例如:

class Foo
{
private:
int m_nValue;
public:
Foo() {};
explicit Foo(int value) : m_nValue(value) {}
int GetValue() const
{
return m_nValue;
}
};

class Bar
{
public:
Bar() {}

static void DoSomething() {
Foo foo;
}
};

Cppcheck 的输出

$ cppcheck --enable=all --inconclusive uninitmembervar.cpp 
[uninitmembervar.cpp:6]: (warning, inconclusive) Member variable 'Foo::m_nValue' is not initialized in the constructor.

关于C++ 静态分析,模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592264/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com