I have some code performing a summation (below). It is called from another file. However, sum
was not initialized, caused a bug but GCC (v11.1) did not give a compiler error.
I have these flags set:
-Wall -Wextra -pedantic -march=native -Werror=return-type -Wswitch-enum -Wconversion -Werror=attributes -Werror=unused-variable -Werror=unused-parameter -Werror=unused-result -Werror=address -Werror=unused-function -Werror=unused-but-set-parameter -Werror=uninitialized -g -O0
Why did I not get an error for this uninitialized local variable/what flag do I need to set?
Are there any other useful flags I should set? (I'm aware of
switch-enum
)
Code:
#include <unordered_set>
#include <concepts>
template<typename T>
concept arithmetic = std::integral<T> or std::floating_point<T>;
template<typename T> requires arithmetic<T>
struct Stat
{
T calculate()
{
T sum; // Not initialized, caused a bug. Compiler gave no error
for(const T& t : m_set)
{
sum += t;
}
// Do other calcs
return sum;
}
// Other methods
std::unordered_set<T> m_set;
};