I need a static analyzer that will find uninitialized variables members/variables... of a templated class type.
Can any analyzer do this? I tried clang/cppcheck and a couple of others with no luck.
Here is my test code:
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
};
I have further distilled the problem to:
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;
}
};
This does not generate an unitialized variable warning, but when I comment out:
//Foo(int value) : m_nValue(value) {}
it does
Thank you for evaluating Cppcheck. A warning is given for the second example, in case you add the --inconclusive flag, for example:
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;
}
};
The output from Cppcheck
$ cppcheck --enable=all --inconclusive uninitmembervar.cpp
[uninitmembervar.cpp:6]: (warning, inconclusive) Member variable 'Foo::m_nValue' is not initialized in the constructor.
Related
I would like to inherit from a class with the const specifier like this:
class Property
{
int get() const;
void set(int a);
};
class ConstChild : public const Property
{
// Can never call A::set() with this class, even if
// the instantiation of this class is not const
};
class NonConstChild : public Property
{
// Can call both A::set() and A::get() depending on
// the constness of instantiation of this class
};
My compiler obviously gives me an error for the const keyword in the second classes declaration. Ideally I'd like to avoid having to create a new class ReadOnlyProperty from which ConstChild would inherit.
Can I somehow use the const keyword for inheritance?
If not, do you have any other ideas on how to solve this problem?
Introduce mutability later in your inheritance tree and derive appropriately:
class Property
{
int get() const;
};
class MutableProperty : public Property {
{
void set(int a);
};
And then:
class ConstChild : public Property { ... };
class MutableChild : public MutableProperty { ... };
I had the need for a related problem, which is: to really control/highlight mutable and const access on some class.
I did it with this simple reusable template wrapper:
template <typename T>
class TAccessor : private T
{
public:
const T& Const() const { return *this; }
T& Mutable() { return *this; }
};
// Example of use:
template <typename T>
using MyVector = TAccessor<std::vector<T>>;
void main()
{
MyVector<int> vector;
vector.Mutable().push_back(10);
int x = vector.Const()[1];
...
}
If you create a const member function set, you will get what you need.
class Property
{
int get() const;
void set(int a);
};
class ConstChild : public Property
{
void set(int a) const {}
};
The only caveat is that a sly user can circumvent your intention by using:
ConstChild child;
child.set(10); // Not OK by the compiler
Property& base = child;
base.set(10); // OK by the compiler
I would like to inherit from a class with the const specifier"
However much you want to is irrelevant. You cannot. That is not valid C++.
Can I somehow use the const keyword for inheritance?"
No.
Use a data member or private base class instead of public base class.
Then you control the access to that member.
You can make the Property thing an abstract interface if you need polymorphic behavior.
You can use a template class and a specialization for a constant type:
template<typename T> class base_property {
protected:
T value;
};
template<typename T> class property : public base_property<T> {
public:
const T& get()const { return value; }
void set(const T& v){ value = v; }
};
template<typename T> class property<const T> : public base_property<T> {
public:
const T& get()const { return value; }
};
class ConstChild : public property<const int>{ };
I had the same need and I ended up with this quite manual approach :
class A
{
public:
void fooMutable(void) {}
void fooConst(void) const {}
};
class B : private A
{
public:
using A::fooConst;
const A& getParent(void) const
{
return *this;
}
};
void fooParent(const A&) {}
int main(void)
{
auto b = B{};
b.fooConst(); // Ok
b.fooMutable(); // Fail
fooParent(b); // Fail
fooParent(b.getParent()); // Ok
return 0;
}
Note that the using keyword would not work with overloads const/mutable :
class A
{
public:
void foo(void) {}
void foo(void) const {}
};
class B : private A
{
public:
using A::foo; // Expose the const and the mutable version
};
To solve this you could redefine the function yourself and call the parent :
class B : private A
{
public:
void foo(void) const
{
A::foo();
}
};
It can become pretty time consuming if you're inheriting a large hierarchy, but if it's for a not-so-big class it should be very reasonable and being quite natural for the user.
I have a trick, not a clean solution.
class ConstChild : private Property
{
operator const Property () { return *this; }
};
then
ConstChild cc;
cc.set(10); // ERROR
cc().get();
I have a problem with the following piece of code that can't even compile.
The problem is on the line
class2_ = new Class2(myFunction);
In fact, I don't now how to reference myFunction, I have also tried class2_ = new Class2(&Class3::myFunction); but the compiler still complains :(
2 constraints:
The function myFunction can't be declared as static
This code will be used in a Arduino base project, I can't use boost::bind
Could you please help me ?
Thanks.
class Class2 {
typedef void(*MyFunction) (int what);
MyFunction fn_;
public:
Class2(MyFunction fn) : fn_(fn) {}
void invoke(int val) {
fn_(val);
}
};
class Class3 {
Class2* class2_;
public:
Class3() {
class2_ = new Class2(myFunction);
class2_->invoke(12);
}
void myFunction(int what) {
// Do some work
}
};
void test2() {
Class3 instance3;
}
How about using an interface for that:
class ClassWithFunction {
public:
virtual void myFunction(int what) = 0;
}
class Class2 {
ClassWithFunction* fn_;
public:
Class2(MyFunction* fn) : fn_(fn) {}
void invoke(int val) {
fn_->myFunction(val);
}
};
class Class3 : ClassWithFunction {
Class2* class2_;
public:
Class3() {
class2_ = new Class2(this);
class2_->invoke(12);
}
void myFunction(int what) {
// Do some work
}
};
You need to use pointer to member function.
class Class3;
class Class2 {
typedef void(Class3::*MyFunction) (int);
MyFunction fn_;
public:
Class2(MyFunction fn) : fn_(fn) {}
void invoke(Class3 *p, int val) {
(p->*fn_)(val);
}
};
class Class3 {
Class2* class2_;
public:
Class3() {
class2_ = new Class2(&Class3::myFunction);
class2_->invoke(this, 12);
}
void myFunction(int what) {
// Do some work
}
};
If you want that Class2 accepts any callable object with signature void(int), then class template, function wrapper, lambda expression etc. will help.
I would like to inherit from a class with the const specifier like this:
class Property
{
int get() const;
void set(int a);
};
class ConstChild : public const Property
{
// Can never call A::set() with this class, even if
// the instantiation of this class is not const
};
class NonConstChild : public Property
{
// Can call both A::set() and A::get() depending on
// the constness of instantiation of this class
};
My compiler obviously gives me an error for the const keyword in the second classes declaration. Ideally I'd like to avoid having to create a new class ReadOnlyProperty from which ConstChild would inherit.
Can I somehow use the const keyword for inheritance?
If not, do you have any other ideas on how to solve this problem?
Introduce mutability later in your inheritance tree and derive appropriately:
class Property
{
int get() const;
};
class MutableProperty : public Property {
{
void set(int a);
};
And then:
class ConstChild : public Property { ... };
class MutableChild : public MutableProperty { ... };
I had the need for a related problem, which is: to really control/highlight mutable and const access on some class.
I did it with this simple reusable template wrapper:
template <typename T>
class TAccessor : private T
{
public:
const T& Const() const { return *this; }
T& Mutable() { return *this; }
};
// Example of use:
template <typename T>
using MyVector = TAccessor<std::vector<T>>;
void main()
{
MyVector<int> vector;
vector.Mutable().push_back(10);
int x = vector.Const()[1];
...
}
If you create a const member function set, you will get what you need.
class Property
{
int get() const;
void set(int a);
};
class ConstChild : public Property
{
void set(int a) const {}
};
The only caveat is that a sly user can circumvent your intention by using:
ConstChild child;
child.set(10); // Not OK by the compiler
Property& base = child;
base.set(10); // OK by the compiler
I would like to inherit from a class with the const specifier"
However much you want to is irrelevant. You cannot. That is not valid C++.
Can I somehow use the const keyword for inheritance?"
No.
Use a data member or private base class instead of public base class.
Then you control the access to that member.
You can make the Property thing an abstract interface if you need polymorphic behavior.
You can use a template class and a specialization for a constant type:
template<typename T> class base_property {
protected:
T value;
};
template<typename T> class property : public base_property<T> {
public:
const T& get()const { return value; }
void set(const T& v){ value = v; }
};
template<typename T> class property<const T> : public base_property<T> {
public:
const T& get()const { return value; }
};
class ConstChild : public property<const int>{ };
I had the same need and I ended up with this quite manual approach :
class A
{
public:
void fooMutable(void) {}
void fooConst(void) const {}
};
class B : private A
{
public:
using A::fooConst;
const A& getParent(void) const
{
return *this;
}
};
void fooParent(const A&) {}
int main(void)
{
auto b = B{};
b.fooConst(); // Ok
b.fooMutable(); // Fail
fooParent(b); // Fail
fooParent(b.getParent()); // Ok
return 0;
}
Note that the using keyword would not work with overloads const/mutable :
class A
{
public:
void foo(void) {}
void foo(void) const {}
};
class B : private A
{
public:
using A::foo; // Expose the const and the mutable version
};
To solve this you could redefine the function yourself and call the parent :
class B : private A
{
public:
void foo(void) const
{
A::foo();
}
};
It can become pretty time consuming if you're inheriting a large hierarchy, but if it's for a not-so-big class it should be very reasonable and being quite natural for the user.
I have a trick, not a clean solution.
class ConstChild : private Property
{
operator const Property () { return *this; }
};
then
ConstChild cc;
cc.set(10); // ERROR
cc().get();
I am doing C++ from 2 years or so , I say why we need constructors in class and not in structures , and why we cant do constructor overloading in structure...
why we need constructors in class
We don't.
// Perfectly valid
class Foo
{
public:
int x
};
why we cant do constructor overloading in structure...
We can.
// Look, ma!
struct Bar
{
Bar operator+(const Bar& other);
};
I don't know where you heard these claims but it certainly wasn't from trying it out.
A user-defined type declared with the keyword struct is a class.
The only difference between struct and class in C++ is visibility; a struct defaults to public while a class defaults to private.
Once you get past that initial visibility, however, they are indistinguishable. A struct is a class with default public visibility.
These two pieces of code have exactly the same effect.
struct MyClass {
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
void setI(int i) { m_i = i; }
private:
int m_i;
};
// is exactly the same as
class MyClass {
public:
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
void setI(int i) { m_i = i; }
private:
int m_i;
};
or put another way
class MyClass {
int m_i;
public:
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
};
struct MyClass {
private:
int m_i;
public:
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
};
I'm getting a C++ compiler error that I don't understand and haven't been able to find
a fix or an explanation for. Here is a code snippet demonstrating the problem.
#include <iostream>
template<class T>
class A {
public:
A(int n) {data = new T[n]; }
const T &operator()(int i) const {return data[i];}
protected:
T *data;
};
template<class T>
class B : public A<T> {
public:
B(int n) : A<T>(n) {}
T &operator()(int i) {return this->data[i]; }
//const T &operator()(int i) const {return this->data[i];} // fixes problem
};
template<class T, int N>
class C : public B<T> {
public:
C() : B<T>(N) {}
private:
};
template<class T>
void doOp(const T &v) {
std::cout << v(0) << std::endl;
}
void templateTest()
{
C<double, 3> c;
c(0) = 5;
std::cout << c(0) << std::endl;
doOp(c);
}
If I un-comment the line in class B, the code compiles and executes correctly but I
don't understand why defining this operator function in class B is any different from
the definition in class A.
Thanks for the help.
Bill
The problem is that doOp() is invoking a non-const member function through a reference to const.
If you uncomment the commented line, a viable const member function will be found, and overload resolution will pick that version of the call operator.
Without uncommenting that line, the inherited version of the call operator is not found because it is being hidden by the overloaded call operator in the subclass.
To illustrate the problem of name hiding with a simpler example, consider the following program:
struct X
{
void foo() { }
};
struct Y : X
{
void foo(int) { }
};
int main()
{
Y y;
y.foo(42); // OK
y.foo(); // ERROR! Name hiding...
}
The compiler here won't be able to resolve the call to y.foo(), because X::foo() is being hidden by Y::foo() here.
To fix the problem, you could add a using declaration in Y:
struct X
{
void foo() { }
};
struct Y : X
{
using X::foo;
// ^^^^^^^^^^^^^
void foo(int) { }
};
int main()
{
Y y;
y.foo(42); // OK
y.foo(); // OK
}
Now both function calls are correctly resolved. In your program, you could add a similar using declaration for operator () of the base class:
template<class T>
class B : public A<T> {
public:
B(int n) : A<T>(n) {}
T &operator()(int i) {return this->data[i]; }
using A<T>::operator();
// ^^^^^^^^^^^^^^^^^^^^^^^
};