When I provide a constructor for class A, I don't get the unreferenced local variable why? What does the empty constructor do to eliminate the warning?
class A
{
public:
A() {}
};
int main()
{
A a;
}
This is only a theory, but because a constructor may contain code that can cause side effects, someone may decide to construct an unused object just to run that code. If you have no constructor and never reference an object that you've constructed, then it can safely be determined that the object has no purpose.
For example if A
is something that holds a mutex lock (and release the lock when destructed), then this code
int main()
{
A a;
// other actions
}
is able to keep this function thread-safe, even a
does not be referenced.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With