| 1 |
// PR c++/69355 |
| 2 |
// { dg-do run } |
| 3 |
|
| 4 |
template <int> struct A; |
| 5 |
template <> struct A<1> {}; |
| 6 |
template <class Obj, int> struct B |
| 7 |
{ |
| 8 |
template <class T> struct C |
| 9 |
{ |
| 10 |
typedef T *iterator; |
| 11 |
C (iterator p1) : m_iter (p1) {} |
| 12 |
void operator, (T p1) { *m_iter = p1; } |
| 13 |
iterator m_iter; |
| 14 |
}; |
| 15 |
typedef double *iterator; |
| 16 |
B (Obj &p1, double) : m_object (p1) {} |
| 17 |
C<double> operator, (double); |
| 18 |
Obj &m_object; |
| 19 |
}; |
| 20 |
template <class Obj, int LEN> |
| 21 |
typename B<Obj, LEN>::template C<double> |
| 22 |
B<Obj, LEN>::operator, (double p1) |
| 23 |
{ |
| 24 |
iterator a = m_object.data (), b = a + 1; |
| 25 |
*a = 1; |
| 26 |
*b = p1; |
| 27 |
return C<double>(b + 1); |
| 28 |
} |
| 29 |
class D {}; |
| 30 |
inline double operator+(const double &p1, D) { return p1; } |
| 31 |
template <int> class U; |
| 32 |
template <int Sz, int K = 0> struct F |
| 33 |
{ |
| 34 |
enum { doIt = K < Sz - 1 ? 1 : 0 }; |
| 35 |
template <class Dest, class Src, class Assign> |
| 36 |
static void assign (Dest &p1, Src &p2, Assign &p3) |
| 37 |
{ |
| 38 |
p3.apply_on (p1 (K), p2 (K)); |
| 39 |
F<Sz * doIt, (K + 1) * doIt>::assign (p1, p2, p3); |
| 40 |
} |
| 41 |
template <class Dest, class Src> static double dot (Dest &p1, Src &p2) |
| 42 |
{ |
| 43 |
return p1 (K) * p2 (K) + F<Sz * doIt, (K + 1) * doIt>::dot (p1, p2); |
| 44 |
} |
| 45 |
}; |
| 46 |
template <> struct F<0> |
| 47 |
{ |
| 48 |
template <class Dest, class Src, class Assign> |
| 49 |
static void assign (Dest &, Src &, Assign &) {} |
| 50 |
template <class Dest, class Src> static D dot (Dest &, Src &) { return D (); } |
| 51 |
}; |
| 52 |
template <class E, int Sz> struct G |
| 53 |
{ |
| 54 |
enum { ops_assign, use_meta }; |
| 55 |
G (const E &p1) : m_expr (p1) {} |
| 56 |
double operator()(int p1) const { return m_expr (p1); } |
| 57 |
template <class Dest, class Src, class Assign> |
| 58 |
static void do_assign (A<1>, Dest &p2, Src &p3, Assign &p4) |
| 59 |
{ |
| 60 |
F<Sz>::assign (p2, p3, p4); |
| 61 |
} |
| 62 |
template <class Dest, class Assign> |
| 63 |
void assign_to (Dest &p1, const Assign &p2) const |
| 64 |
{ |
| 65 |
do_assign (A<1>(), p1, *this, p2); |
| 66 |
} |
| 67 |
E m_expr; |
| 68 |
}; |
| 69 |
struct H |
| 70 |
{ |
| 71 |
static double apply_on (double p1, long double p2) { return p1 / p2; } |
| 72 |
static void apply_on (double &p1, double p2) { p1 = p2; } |
| 73 |
}; |
| 74 |
template <class E1, class E2> struct I |
| 75 |
{ |
| 76 |
I (const E1 &p1, const E2 &p2) : m_lhs (p1), m_rhs (p2) {} |
| 77 |
double operator()(int p1) const |
| 78 |
{ |
| 79 |
double c = m_lhs (p1); |
| 80 |
return H::apply_on (c, m_rhs (0)); |
| 81 |
} |
| 82 |
E1 m_lhs; |
| 83 |
const E2 m_rhs; |
| 84 |
}; |
| 85 |
struct J |
| 86 |
{ |
| 87 |
J (double p1) : m_data (p1) {} |
| 88 |
long double operator()(int) const { return m_data; } |
| 89 |
long double m_data; |
| 90 |
}; |
| 91 |
template <int Sz> struct K |
| 92 |
{ |
| 93 |
K (const U<Sz> &p1) : m_data (p1.data ()) {} |
| 94 |
double operator()(int p1) const { return m_data[p1]; } |
| 95 |
const double *m_data; |
| 96 |
}; |
| 97 |
template <int Sz> struct U |
| 98 |
{ |
| 99 |
U () {} |
| 100 |
U (const U &p1) |
| 101 |
{ |
| 102 |
*this = G<ConstReference, Sz>(p1.const_ref ()); |
| 103 |
} |
| 104 |
B<U, Sz> operator=(double) { return B<U, Sz>(*this, 0); } |
| 105 |
double *data () { return m_data; } |
| 106 |
const double *data () const { return m_data; } |
| 107 |
double &operator()(int p1) { return m_data[p1]; } |
| 108 |
double operator()(int p1) const { return m_data[p1]; } |
| 109 |
typedef K<Sz> ConstReference; |
| 110 |
ConstReference const_ref () const { return *this; } |
| 111 |
template <class E> void operator=(const G<E, Sz> &p1) |
| 112 |
{ |
| 113 |
p1.assign_to (*this, H ()); |
| 114 |
} |
| 115 |
double m_data[Sz]; |
| 116 |
}; |
| 117 |
template <int Sz> |
| 118 |
G<I<K<Sz>, J>, Sz> div (U<Sz> &p1, double p2) |
| 119 |
{ |
| 120 |
typedef I<K<Sz>, J> expr_type; |
| 121 |
return G<expr_type, Sz>(expr_type (p1.const_ref (), p2)); |
| 122 |
} |
| 123 |
template <int Sz> double norm2 (U<Sz> &p1) |
| 124 |
{ |
| 125 |
return __builtin_sqrt (F<Sz>::dot (p1, p1)); |
| 126 |
} |
| 127 |
template <int Sz> |
| 128 |
G<I<K<Sz>, J>, Sz> operator/(U<Sz> &p1, double p2) |
| 129 |
{ |
| 130 |
return div (p1, p2); |
| 131 |
} |
| 132 |
typedef U<3> V; |
| 133 |
V foo (V p1) |
| 134 |
{ |
| 135 |
double e = norm2 (p1); |
| 136 |
V r; |
| 137 |
r = p1 / e; |
| 138 |
return r; |
| 139 |
} |
| 140 |
int |
| 141 |
main () |
| 142 |
{ |
| 143 |
V f; |
| 144 |
f = 1, 2, 3; |
| 145 |
V r = foo (f); |
| 146 |
if (__builtin_fabs (r (0) - 0.267261) > 0.01 |
| 147 |
|| __builtin_fabs (r (1) - 0.534522) > 0.01 |
| 148 |
|| __builtin_fabs (r (2) - 0.801784) > 0.01) |
| 149 |
__builtin_abort (); |
| 150 |
} |