Explanation of call ordering in a cout statement, postfix vs prefix - c++

I was overloading the postfix and prefix ++ operator and testing them out, when I noticed something unexpected in the actual value output when printing out my test. As an example, here is an integer test:
int i = 0;
cout << "before prefix: " << i << " prefixing.. " << ++i << " after prefix. " << i << endl;
In my mind, this should print out "before prefix: 0 prefixing... 1 after prefix. 1"
To my dismay, this prints before prefix: 1 prefixing.. 1 after prefix. 1
Why is it already 1 at the start of the call?! Ok, maybe it parses through the statement before printing and i gets incremented before the printing even begins.
But then I tested the postfix integer incrementing...
int i = 0;
cout << "before postfix: " << i << " postfixing.. " << i++ << " after postfix. " << i << endl;
before postfix: 1 postfixing.. 0 after postfix. 1
The increment happens everywhere but the middle statement?! This is quite counter intuitive. Can somebody please shed some light on this?

Reading and incrementing a variable in the same statement (or, technically, between two sequence points, in this case, the semicolons) causes undefined behaviour - in other words, the compiler is allowed to do whatever it wants.

Related

Is there a way to set the value of a variable to empty in C++?

I have a program which prints a statement based on inputs. The print statement prints using all inputs, but I only want it to print inputs which are actually given. For instance:
std::cout << "-Number: " << number < "-Letter: " << letter << "-String: " << str << "-Sum: " << sum << std::endl;
(for the purpose of demonstration, the variables above are arbitrary, this is just to show a point)
So this print statement is called after every iteration of a loop. The values can be anything, but I do not want it to print if a value was not received. Ideally, this would be done like so:
// Get input... Then print statement...
// If input was not received for a value (i.e. it equals none) then skip that value
std::cout << "-Number: " << number < "-Letter: " << letter << "-String: " << str << "-Sum: " << sum << std::endl;
number = none, letter = none, str = none, sum = none // reset inputs and repeat loop
(Once again, the above is pseudo-code for the purpose of demonstration)
Is this possible in C++?
If you have C++17 or a later version see optional. Prior to that, you would need to wrap your type into a proper struct that would mimic what optional does (of which you can find various implementation on the net, e.g. https://github.com/TartanLlama/optional).
In your example, you could do:
std::optional<int> number;
// Puts a value inside number
if (some condition)
number.value() = 44;
// Set number to invalid
else number.reset();
std::cout << "Number: " << (number ? number.value() : "Some Default Value You want to Show" << std::endl;
std::optional has an operator bool() which tells you if you correctly have a value in your std::optional. Also note that your std::optional is invalid by default if you call the default constructor (see https://en.cppreference.com/w/cpp/utility/optional/optional).

C++ single quotes syntax

I am learning C++ and just started reading "Programming Principles and Practice" by Bjarne Stroustrup and he uses this code to illustrate a point:
#include "std_lib_facilities.h"
using namespace std;
int main() // C++ programs start by executing the function main
{
char c = 'x';
int i1 = c;
int i2 = 'x';
char c2 = i1;
cout << c << ' << i1 << ' << c2 << '\n';
return 0;
}
I am familiar in general with the difference between double and single quotes in the C++ world, but would someone kindly explain the construction and purpose of the section ' << i1 << '
Thanks
cout << c << ' << i1 << ' << c2 << '\n';
appears to be a typo in the book. I see it in Programming Principles and Practice Using C++ (Second Edition) Second printing. I do not see it listed in the errata.
According to the book, the intended output is
x 120 x
But what happens here is ' << i1 << ' attempts to compress the << i1 << to a multi-byte character and prints out an integer (most likely 540818464-> 0x203C3C20 -> ASCII values of ' ', '<', '<', ' ') because cout doesn't know wide characters. You'd need wcout for that. End result is output something like
x540818464x
and a warning or two from the compiler because while it's valid C++ code, it's almost certainly not what you want to be doing.
The line should most likely read
cout << c << ' ' << i1 << ' ' << c2 << '\n';
which will output the expected x 120 x
In other words, Linker3000, you are not crazy and not misunderstanding the example code.
Anyone know who I should contact to log errata or get a clarification on the off chance there is some top secret sneakiness going way over my head?
Before answering your question, here is a little background on what that is actually doing. Also note that there is a typo in the example, the string constant should have been double quoted:
cout << c << " << i1 << " << c2 << "\n";
In C++, operators can be overloaded so that they mean different things with different functions. In the case of cout, the << operator is overloaded as the "Insertion Operator". Think of it as taking the operand on the right, and inserting it (or sending it) into the operator on the left.
For example,
cout << "Hello World";
This takes the string "Hello World", and sends it to cout for processing.
So what beginners do not get is what something like this means:
cout << "Hello" << " World";
This is doing the same thing, but the operator precedence says to perform the injections from left to right. To make this work, the cout object returns itself as a function return value. Why is this important? Because the above statement is actually two separate operator evaluations:
(cout << "Hello") << " World";
This first injects "Hello" to cout, which outputs it, then continues to evaluate the next inject operator. Because cout returns itself, after the (cout << "Hello") is executed you have the following still to be evaluated:
cout << " World";
This expression injects " World" into the cout object, which then outputs " World", with the net effect being that you see "Hello World" just like the first time.
So in your example, what is it doing?
cout << c << " << i1 << " << c2 << "\n";
This is evaluated left to right as follows:
((((cout << c) << " << i1 << ") << c2) << "\n"); => Outputs value of c
((((cout ) << " << i1 << ") << c2) << "\n"); => Outputs string " << i1 << "
((( cout ) << c2) << "\n"); => Outputs value of c2
(( cout ) << "\n"); => Outputs newline character
( cout ); => No more output
Expression completes and returns the cout object as the expression value.
Assuming c='x' and c2='x', the final output from this expression is the following character string output on a single line:
x << i1 << x
For beginners, all those insertion operators << look a little strange. It is because you are dealing with objects. You could build the string up as a complete formatted object before injecting it into cout, and while that make the cout expression look simpler, we do not do that in C++ because it makes your code more complex and error prone. Note also, there is nothing special about the cout object. If you wanted to output to the standard error stream, you would use cerr instead. If you wanted to output to a file, your would instantiate a stream object that outputs to the desired file. That rest of the code in your example would be the same.
In C, the same thing would be done procedurally using a format string:
printf("%d << i1 << %d\n", i1, c2);
This is allowed in C++ too, because C++ is a superset of C. Many C++ programmers still use this output method, but that is because those programmers learned C first, and may not have fully embraced the object oriented nature of C++
Note that you may also have seen the << operator in the context of mathematical expressions like:
A = A << 8;
In this case, the << operator is the bitwise rotate operation. It has nothing to do with output to cout. It will rotate the bits in A to the left by eight bits.

C++ Array passed by reference, but how to understand this?

The arrays are passed by reference. Any changes made to the array within the function changeArray will be observed in the calling scope (main function here).
However the codes below print 0 1 in the 1st cout, and print 2 in the 2nd "cout". What I don't understand is that why the first cout prints the original value of array[0]=1 instead of the changed value of array[0]=2?
Thanks a lot.
#include <iostream>
using namespace std;
int changeArray(int array[]) {
array[0]=2*array[0];
return 0;
}
int main() {
int array[]={1,2,3,4};
cout << changeArray(array) << " " << array[0] << endl;
cout << array[0] << endl;
return 0;
}
To make sure that the compiler doesn't reorder the execution:
cout << array[0] << endl;
changeArray(array);
cout << array[0] << endl;
This prints 1 and then 2.
The C++ compiler is allowed to optimize the code by reordering the execution of code within a single expression (e.g. cout << changeArray(array) << " " << array[0] << endl). To avoid that, and to make sure changeArray gets called first, you need to split your expression to separate statements, e.g. by using the semicolon (;). Everything before the semicolon gets executed before anything after the semicolon can start.

Why does multiple calls to xalloc result in delayed output?

When I print the id of a stream in a single expression it prints it backwards. Normally this is what comes out:
std::stringstream ss;
std::cout << ss.xalloc() << '\n';
std::cout << ss.xalloc() << '\n';
std::cout << ss.xalloc();
Output is:
4
5
6
But when I do it in one expression it prints backwards, why?
std::stringstream ss;
std::cout << ss.xalloc() << '\n'
<< ss.xalloc() << '\n'
<< ss.xalloc();
Output:
6
5
4
I know the order of evaluation is unspecified but then why does the following always result in the correct order:
std::cout << 4 << 5 << 6;
Can someone explain why xalloc behaves differently? Thanks.
This isn't related to xalloc; any other function that returns a different value each time it's called would do the same thing. The order of evaluation of the arguments is unspecified; the compiler can make the three function calls in any order. Once all the arguments have been evaluated, the order of insertion is from left to right.

C++ const casting

I am trying to print the value of a const but it is not working. I am making a return to C++ after years so I know casting is a possible solution but I can't get that working either.
The code is as follows:
//the number of blanks surrounding the greeting
const int pad = 0;
//the number of rows and columns to write
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl << "Rows : " + rows;
I am trying to print the value of 'rows' without success.
You want:
cout << endl << "Rows : " << rows;
Note this has nothing to do with const - C++ does not allow you to concatenate strings and numbers with the + operator. What you were actually doing was that mysterious thing called pointer arithmetic.
You're almost there:
cout << endl << "Rows : " << rows;
The error is because "Rows : " is a string literal, thus is a constant, and generally speaking is not modified as you may think.
Going slightly further, you likely used + (colloquially used as a concatenation operation) assuming you needed to build a string to give to the output stream. Instead operator << returns the output stream when it is done, allowing chaining.
// It is almost as if you did:
(((cout << endl) << "Rows : ") << rows)
I think you want:
std::cout << std::endl << "Rows : " << rows << std::endl;
I make this mistake all the time as I also work with java a lot.
As others have pointed out, you need
std::cout << std::endl << "Rows : " << rows << std::endl;
The reason (or one of the reasons) is that "Rows : " is a char* and the + operator for char*s doesn't concatenate strings, like the one for std::string and strings in languages like Java and Python.