Order of << operator.

Started by
1 comment, last by beebs1 12 years, 2 months ago
Hiya,

I've written a couple of wrapper classes for ODBC, and I'm using the << operator in a test application.


while(resultSet->fetchNext())
{
std::cout << "ID " << resultSet->getUInt32(1) << ", " << resultSet->getString(2) << "\n";
}


This is causing an ODBC error, and I believe it's because the API requires me to access columns in increasing order.. column 1, column 2, etc.

I can see that the code is actually calling the get...() methods from right to left, which is the complete opposite of what I've always assumed...

Does anyone know a way of making it call from 'left to right' instead, without splitting into multiple lines?

Many thanks for any suggestions!
Advertisement
The order of evaluation of subexpressions within an expression is undefined. Only between sequence points can you guarantee order of evaluation (expressions before a sequence point are guaranteed to be evaluated any expression after a sequence point). That is, it is possible for getString(2) to be evaluated before getString(1), but don't mistake that for one being printed before the other one though; the order of what is passed through the stream is well defined and guaranteed.

There is no real way to guarantee the order of evaluation inside an expression. But what is the problem with splitting it in multiple lines?
[source]
while(resultSet->fetchNext())
{
unsigned int r1 = resultSet->getUInt32(1);
unsigned int r2 = resultSet->getUInt32(2);
std::cout << "ID " << r1 << ", " << r2 << "\n";
}

[/source]
No problem really.

Thanks, I didn't know that.

This topic is closed to new replies.

Advertisement