The following code works but I would like to avoid the warning:
warning: 'fitness::vect_' should be initialized in the member initialization list [-Weffc++]
when it is compiled with the g++ -Weffc++
switch:
#include <array>
template<class T, unsigned N>
class fitness
{
public:
explicit fitness(T v)
{
static_assert(N, "fitness zero length");
vect_.fill(v);
}
private:
std::array<T, N> vect_;
};
int main()
{
fitness<double, 4> f(-1000.0);
return 0;
}
Should I ignore the warning? Is there a way to fill vect_
in the constructor initialization list (without changing its type)?
I believe you can ignore this warning.
It works if you place an empty initialization for the array in the constructor:
#include <array>
template<class T, unsigned N>
class fitness
{
public:
explicit fitness(T v):
vect_{}
{
static_assert(N, "fitness zero length");
vect_.fill(v);
}
private:
std::array<T, N> vect_;
};
int main()
{
fitness<double, 4> f(-1000.0);
return 0;
}
Try to use
explicit fitness(T v) : vect_{}
{
//...
}
The default constructor (read: value initializer) should work fine in this case. As std::array
is an aggregate type, its elements will be each be value-initialized, which for numeric types like double
means zero-initialization, and then you can use fill
.
explicit fitness(T v) : vect_() // or vect_{}
{
vect_.fill(v);
}
You might be better off using std::vector
and its fill constructor if you don't want to do essentially double the initialization, though. Then your class would become:
template<class T>
class fitness
{
public:
explicit fitness(T v, unsigned n) : vect_(n, v)
private:
std::vector<T> vect_;
};
int main()
{
fitness<double> f(-1000.0, 4);
return 0;
}
You could, of course, still keep the N
as a template parameter, but there's no need to do so as the length doesn't need to be known at compile-time. (On the other hand, if you stick with std::array
you might be able to set up the constructor as a constexpr
, though that may require some template fiddling or an auxiliary constexpr
function that returns an initializer list to work right, I haven't played around enough with C++11 concepts to know.)
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