Dear GCC gurus, when I compile the attached program with g++ -Wall -Wextra -O2 test.cc I get the following output: test.cc: In function ‘int main()’: test.cc:198: warning: ‘point.Tiny_vector<double, 1>::x[0u]’ may be used uninitialized in this function test.cc:198: note: ‘point.Tiny_vector<double, 1>::x[0u]’ was declared here That's with gcc 4.4.4. Is this warning unjustified or am I missing some subtlety? It's weird that the warning is only given for foo<1>. Also with gcc 4.6.0 I get the following warning test.cc: In function 'Tiny_vector<double, D> foo() [with int D = 1]': test.cc:198:28: warning: 'point.Tiny_vector<double, 1>::x[i.2]' may be used uninitialized in this function [-Wuninitialized] when compiling with g++ -g -Wall -Wextra -O2 test.cc (without -g, there is no warning!) Thanks! Christoph **************************************************************** #include <iostream> #include <cstdlib> #include <boost/static_assert.hpp> template <class T, int D> class Tiny_vector { public: Tiny_vector(); Tiny_vector(T _x); template <class Array> explicit Tiny_vector(Array _x); Tiny_vector(T x0, T x1); Tiny_vector(T x0, T x1, T x2); Tiny_vector(T x0, T x1, T x2, T x3); Tiny_vector(T x0, T x1, T x2, T x3, T x4); T &operator[](unsigned i) { return x[i]; } const T &operator[](unsigned i) const { return x[i]; } Tiny_vector<T, D> &operator+=(const Tiny_vector<T, D> &y); Tiny_vector<T, D> &operator-=(const Tiny_vector<T, D> &y); Tiny_vector<T, D> &operator*=(const T &y); Tiny_vector<T, D> &operator*=(const Tiny_vector<T, D> &y); Tiny_vector<T, D> &operator/=(const T &y); Tiny_vector<T, D> &operator/=(const Tiny_vector<T, D> &y); template <class Scalar_function> Tiny_vector<T, D> &map(Scalar_function sf); private: T x[D]; }; template <class T, int D> Tiny_vector<T, D>::Tiny_vector() { } template <class T, int D> Tiny_vector<T, D>::Tiny_vector(T _x) { for (int i = 0; i < D; ++i) x[i] = _x; } template <class T, int D> template <class Array> Tiny_vector<T, D>::Tiny_vector(Array _x) { for (int i = 0; i < D; ++i) x[i] = _x[i]; } template <class T, int D> Tiny_vector<T, D>::Tiny_vector(T x0, T x1) { BOOST_STATIC_ASSERT(D == 2); x[0] = x0; x[1] = x1; } template <class T, int D> Tiny_vector<T, D>::Tiny_vector(T x0, T x1, T x2) { BOOST_STATIC_ASSERT(D == 3); x[0] = x0; x[1] = x1; x[2] = x2; } template <class T, int D> Tiny_vector<T, D>::Tiny_vector(T x0, T x1, T x2, T x3) { BOOST_STATIC_ASSERT(D == 4); x[0] = x0; x[1] = x1; x[2] = x2; x[3] = x3; } template <class T, int D> Tiny_vector<T, D>::Tiny_vector(T x0, T x1, T x2, T x3, T x4) { BOOST_STATIC_ASSERT(D == 5); x[0] = x0; x[1] = x1; x[2] = x2; x[3] = x3; x[4] = x4; } template <class T, int D> Tiny_vector<T, D> &Tiny_vector<T, D>::operator+=(const Tiny_vector &y) { for (int i = 0; i < D; ++i) x[i] += y[i]; return *this; } template <class T, int D> Tiny_vector<T, D> &Tiny_vector<T, D>::operator-=(const Tiny_vector &y) { for (int i = 0; i < D; ++i) x[i] -= y[i]; return *this; } template <class T, int D> Tiny_vector<T, D> &Tiny_vector<T, D>::operator*=(const T &y) { for (int i = 0; i < D; ++i) x[i] *= y; return *this; } template <class T, int D> Tiny_vector<T, D> &Tiny_vector<T, D>::operator*=(const Tiny_vector &y) { for (int i = 0; i < D; ++i) x[i] *= y[i]; return *this; } template <class T, int D> Tiny_vector<T, D> &Tiny_vector<T, D>::operator/=(const T &y) { for (int i = 0; i < D; ++i) x[i] /= y; return *this; } template <class T, int D> Tiny_vector<T, D> &Tiny_vector<T, D>::operator/=(const Tiny_vector &y) { for (int i = 0; i < D; ++i) x[i] /= y[i]; return *this; } template <class T, int D> std::ostream &operator<<(std::ostream &s, const Tiny_vector<T, D> &a) { for (int i = 0; i < D - 1; ++i) s << a[i] << ' '; s << a[D - 1]; return s; } template <class T, int D> Tiny_vector<T, D> operator+(Tiny_vector<T, D> a, const Tiny_vector<T, D> &b) { return a += b; } template <class T, int D> Tiny_vector<T, D> operator-(Tiny_vector<T, D> a, const Tiny_vector<T, D> &b) { return a -= b; } template <class T, int D, class O> Tiny_vector<T, D> operator*(Tiny_vector<T, D> a, const O &b) { return a *= b; } template <class T, int D> Tiny_vector<T, D> operator*(const T &a, Tiny_vector<T, D> b) { return b *= a; } template <class T, int D, class O> Tiny_vector<T, D> operator/(Tiny_vector<T, D> a, const O &b) { return a /= b; } template <class T> Tiny_vector<T, 3> cross(const Tiny_vector<T, 3> &a, const Tiny_vector<T, 3> &b) { return Tiny_vector<T, 3>(a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]); } template <class T, int D> T dot(const Tiny_vector<T, D> &a, const Tiny_vector<T, D> &b) { T ret = 0; for (int i = 0; i < D; ++i) ret += a[i] * b[i]; return ret; } template<class T, int D> template<class Scalar_function> Tiny_vector<T, D>& Tiny_vector<T, D>::map(Scalar_function sf) { for (int i = 0; i < D; ++i) x[i] = sf(x[i]); return *this; } template <int D> Tiny_vector<double, D> foo() { Tiny_vector<double, D> point; do { for (int i = 0; i < D; ++i) point[i] = double(rand()) / RAND_MAX; } while(dot(point, point) > 1); return point; } int main() { std::cout << foo<1>() << std::endl; std::cout << foo<2>() << std::endl; }