C++ questions

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

mar
Posts: 2552
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: C++ stringstream

Post by mar »

So if you wrote string("go wtime") + ..., it should work fine.
Of course this isn't quite true, it will only work for std::strings, string literals and character literals (which are of type char in C++), so passing an int woud probably result in compile-time error.
EDIT: using a sequence of += instead should be much better performance-wise.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: C++ stringstream

Post by Sven »

mar wrote:
So if you wrote string("go wtime") + ..., it should work fine.
Of course this isn't quite true, it will only work for std::strings, string literals and character literals (which are of type char in C++), so passing an int woud probably result in compile-time error.
EDIT: using a sequence of += instead should be much better performance-wise.
Even with += you still don't get the expected result. Using integers in a string context frequently causes trouble. Sometimes it is due to implicit type conversion rules, sometimes there are other reasons. The C++ type system is quite strict and consistent but certainly not easy to understand.

Since neither + nor += do work here (with integers involved) it is necessary to either use sprintf() or string streams.

Sven
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: C++ stringstream

Post by lucasart »

mar wrote:
using a sequence of += instead should be much better performance-wise.
With the C++11 move semantic, implemented all over the STL, this should not be true anymore. So when you write:
s = s1 + s2 + s3;
the subsequent calls to operator+ do not incurr some temporary object and copying all over the place.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
mar
Posts: 2552
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: C++ stringstream

Post by mar »

lucasart wrote:
mar wrote: using a sequence of += instead should be much better performance-wise.
With the C++11 move semantic, implemented all over the STL, this should not be true anymore. So when you write:
s = s1 + s2 + s3;
the subsequent calls to operator+ do not incurr some temporary object and copying all over the place.
I look forward to it, but it will take some time until all major compilers support C++11 (all of it and without bugs).
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: C++ stringstream

Post by Rein Halbersma »

mar wrote:
lucasart wrote:
mar wrote: using a sequence of += instead should be much better performance-wise.
With the C++11 move semantic, implemented all over the STL, this should not be true anymore. So when you write:
s = s1 + s2 + s3;
the subsequent calls to operator+ do not incurr some temporary object and copying all over the place.
I look forward to it, but it will take some time until all major compilers support C++11 (all of it and without bugs).
move semantics already works in vc++ 2010