------- Comment #3 from rguenth at gcc dot gnu dot org 2006-04-28 20:22 -------
Ok, this one does it, just for the curious:
template <int Dim>
struct Loc;
struct LocStorage
{
int x;
};
template <>
struct Loc<1> : public LocStorage
{
Loc() {}
Loc(int i) { this->x = i; }
Loc<1>& operator[](int) { return *this; }
int get() { return this->x; }
};
template <int Dim>
struct WrapLoc1 : public Loc<1>
{
WrapLoc1() {}
WrapLoc1(int i) : Loc<1>(i) {}
};
template <int Dim>
struct WrapLocN : public Loc<Dim-1>
{
WrapLocN() {}
WrapLocN(int i) : Loc<Dim-1>(i) {}
};
template <int Dim>
struct Loc : public WrapLocN<Dim>, public WrapLoc1<Dim>
{
Loc() {}
Loc(int i) : WrapLocN<Dim>(i), WrapLoc1<Dim>(i) {}
Loc<1>& operator[](int i)
{
if (i == Dim-1)
{
WrapLoc1<Dim> *w = this;
return *w;
}
else
{
WrapLocN<Dim> *sl = this;
return sl->operator[](i-1);
}
}
};
int foo(void)
{
Loc<4> l(1);
return l[0].get() + l[1].get() + l[2].get() + l[3].get();
}
now, we can optimize foo() to return 4, starting with gcc 4.0.
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27181