[LC++]Weird behavior

Torsten Rennett Torsten at Rennett.de
Wed Feb 25 16:06:02 UTC 2004


On Mittwoch, 25. Februar 2004 07:59, Peter Poulsen wrote:
> I made this little tokenizer, which give me unexpected result. Does
> anybody know why? (I compiled it with 2.95 so it may require minor
> modification to run with 3.2. Please bare with me.)
>
> ----------------- main.cc --------------------------
> #include <tokenizer.hh>
> #include <string>
> #include <iostream>
>
> int main()
> {
>         std::Tokenizer t(" \n");
>         std::string line = "one two";
>         t.tokenize(line);
>         // I expect to get first one, then two. It seems to work
>         std::cout << t.next_token() << endl;
>         std::cout << t.next_token() << endl;
>
>         t.tokenize(line);
>         // I expect to get "onetwo", but gets "twoone"! Why?
>         std::cout << t.next_token() << t.next_token() << endl;
>         return 0;
> }

The line 

	cout << t.next_token() << t.next_token();

will be processed by the compiler like this:

	operator<<(operator<<(cout, t.next_token()), t.next_token());

According to the C++-Standard the evaluation of function arguments is
not defined and free to the compiler implementation. So in every
construct like this
	  fct1(fctA(), fctB());
you can't tell if fctA or fctB is evaluated first. In your case fctB is
evaluated first.

Torsten 

-- 
Ingenieurbuero RENNETT      -- innovative Software-Entwicklung --
Torsten Rennett                    http://www.RENNETT.de
Ludwig-Thoma-Weg 14         E-Mail:     mailto:Torsten at Rennett.de
D-85551 Heimstetten         Telefon:              +49-89-90480538




More information about the tuxCPProgramming mailing list