Quite often I rediscover my own old posts and learn new things from it. This time I am revisiting the very first post http://cpptruths.blogspot.com/2005_06_19_cpptruths_archive.html
I came up with a puzzle to "entertain" you guys! Predict the output of following program. You know where to look at for the explanation.
const int FIRST_TIME = 1;
template <typename T>
void func (T &)
{
static int var;
++var;
if (FIRST_TIME == var)
cout << "Printed once." << endl;
else
cout << "Printed more than once." << endl;
}
int main(void)
{
int a1[4];
int a2[5];
func (a1);
func (a2);
}
OUTPUT:
Printed once.
Printed once.
!!
I would rather have a static checker to guard me against such subtle things.
I came up with a puzzle to "entertain" you guys! Predict the output of following program. You know where to look at for the explanation.
const int FIRST_TIME = 1;
template <typename T>
void func (T &)
{
static int var;
++var;
if (FIRST_TIME == var)
cout << "Printed once." << endl;
else
cout << "Printed more than once." << endl;
}
int main(void)
{
int a1[4];
int a2[5];
func (a1);
func (a2);
}
OUTPUT:
Printed once.
Printed once.
!!
I would rather have a static checker to guard me against such subtle things.
Comments