This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/44069] optimization bug initializing from cast array



------- Comment #3 from paolo dot carlini at oracle dot com  2010-05-11 00:44 -------
Bah... let's see what Richard thinks. Personally, I would have written it like
this in the first place:

#include <iostream>

template <unsigned R, unsigned C>
class M {
  public:
    M( const double arr[R][C] ) {
      for (unsigned r = 0; r < R; ++r)
        for (unsigned c = 0; c < C; ++c)
          m[r*C+c] = arr[r][c];
    }
    double operator()(unsigned r, unsigned c) const
      { return m[r*C+c]; }
  private:
    double m[R*C];
};

template <unsigned R, unsigned C>
std::ostream& operator<<( std::ostream& str, const M<R,C>& m )
{
  for (unsigned r = 0; r < R; ++r) {
    str << "{" << m(r,0);
    for (unsigned c = 1; c < C; ++c)
      str << ", " << m(r,c);
    str << "}" << std::endl;
  }
  return str;
}

int main()
{
  double vals[4][4] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 0, 1, 2 },
                        { 3, 4, 5, 6 } };
  M<4,4> m( vals );
  std::cout << m << std::endl;
  return 0;
}


-- 

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu dot
                   |                            |org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44069


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]