This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project. See the libstdc++ home page for more information.
Consider the following code from std_valarray.h:
template<typename _Tp>
inline _Meta
<_BinaryExpression<valarray<_Tp>, _Constant<_Tp>, name##<_Tp> >,
_Tp>
operator##op (const valarray<_Tp> &__v, const _Tp &__t)
{
typedef _BinaryExpression
<valarray<_Tp>, _Constant<_Tp>, name##<_Tp> > _Expr;
return _Meta<_Expr, _Tp> (_Expr (__v, _Constant<_Tp> (__t)));
}
What happens? A temporary of type _Constant<_Tp> will be created. With
that temporary a _BinaryExpression is constructed. The _BinaryExpression
stores just *references* to its arguments. After return from the
function the temporary _Constant will be destroyed, rendering the
reference invalid.
The bug is triggered only if the temporary on the stack actually has
been overwritten by anything else, and that depends on too many factors.
That's why I can't provide example code that triggers the bug reliably.
BTW, reading the code is unnecessary complicated because of the cryptic
identifier names. Why don't you use readable names, at least for local
variables, at least during the development? I know, ugly names are
necessary for standard compliance, but that is less important than
making the code robust. And this latter task is complicated enough, one
doesn't need to make it even more difficult. After finishing the code
`uglifying' is a matter of just some hours.
--
Thomas Kunert