clang-format stream output - c++

clang-format formats my code as follow:
std::cout << 1 << 1 << 1 << 1 << 1 << 1
<< 1 << 1 << 1 << 1 << 1 << 1;
but my style-guide requires:
std::cout << 1 << 1 << 1 << 1 << 1 << 1 <<
1 << 1 << 1 << 1 << 1 << 1;
I tried setting BreakBeforeBinaryOperators:None and AlignOperands:false, but it didn't help me. Is there any other options?

Related

Cin doesn't pause for input, despite using both cin.clear() and cin.ignore()

This input is redirected from file to the executable in a unix terminal:
10 1 2 3 4 5 6 7 8 9 10
5 10 20 30 40 50
Cin doesn't wait for input even though it seems to be in a good state to do so.
int main(void) {
std::cout << "GOOD: " << std::cin.good() << std::endl
<< "EOF: " << std::cin.eof() << std::endl
<< "FAIL: " << std::cin.fail() << std::endl
<< "BAD: " << std::cin.bad() << "\n\n";
int s;
while(std::cin >> s) {
int * arr = new int[s];
for(int i = 0; i < s; i++) {
std::cin >> arr[i];
std::cout << arr[i] << " ";
}
std::cout << "\n\n";
delete[] arr;
}
std::cout << "CAN READ: " << (bool)(std::cin >> s) << std::endl
<< "GOOD: "<< std::cin.good() << std::endl
<< "EOF: "<< std::cin.eof() << std::endl
<< "FAIL: "<< std::cin.fail() << std::endl
<< "BAD: "<< std::cin.bad() << "\n\n";
std::cin.clear();
std::cin.ignore(INT_MAX);
std::cout << "GOOD: " << std::cin.good() << std::endl
<< "EOF: " << std::cin.eof() << std::endl
<< "FAIL: " << std::cin.fail() << std::endl
<< "BAD: " << std::cin.bad() << "\n\n";
std::cin.clear();
std::cout << "GOOD: " << std::cin.good() << std::endl
<< "EOF: " << std::cin.eof() << std::endl
<< "FAIL: " << std::cin.fail() << std::endl
<< "BAD: " << std::cin.bad() << "\n\n";
std::cout << "Enter a #:\n";
std::cin >> s; // unable to provide input (EDIT: from keyboard) here
std::cout << s << std::endl;
std::cout << "Program ends.\n";
return 0;
}
The output is
GOOD: 1
EOF: 0
FAIL: 0
BAD: 0
1 2 3 4 5 6 7 8 9 10
10 20 30 40 50
CAN READ: 0
GOOD: 0
EOF: 1
FAIL: 1
BAD: 0
GOOD: 0
EOF: 1
FAIL: 0
BAD: 0
GOOD: 1
EOF: 0
FAIL: 0
BAD: 0
Enter a #:
5
Program ends.
What am I missing here? Why doesn't cin allow me to provide it input even though its buffer is empty and the stream is in a good state? What can I do to get cin to accept further input here?
Thought it was possible to seamlessly obtain keyboard input after redirecting a file to cin (thanks to Artemy Vysotsky for helping me realize that this is not the case).
For anyone that has a similar problem, std::cin.rdbuf() can be used. Details here: Can std::cin switch from accepting file input to keyboard input at run-time?
use cin.flush(); after taking input an integer. It will flush the stream and you can take the string input again.
If you want to know why it happens you can read it here

g++ template error Small_size

I am working on the latest revision of the C++ programming language (think it's 5) and run into a problem with g++ version 5.2.
My code is a variation of Small_size template from chap 24.
#include <iostream>
template<int N>
bool is_small ()
{
std::cerr << sizeof(N) << std::endl;
std::cerr << N << std::endl;
return N <= 255;
}
bool ism (int i_n)
{
return i_n <= 255;
}
int main ()
{
std::cout << "hallo welt" << std::endl;
std::cout << 0 << " " << is_small<0> << std::endl;
std::cout << 255 << " " <<is_small<255> << std::endl;
std::cout << -4100000000 << " " << is_small<-4100000000> << std::endl;
std::cout << 256 << " " << is_small<256> << std::endl;
std::cout << 256 << " " << ism(256) << std::endl;
std::cout << 256 << " " << (256 <= 255) << std::endl;
}
When I compile it, it's ok. But when I run the thing, it simply seems to be broken.
[cpp11#hydra src]$ cat ~/bin/g14
#!/bin/bash
g++-52 -std=c++14 "${1}.C" -L$LIBPATH -o "$1"
[cpp11#hydra src]$ g14 konzept_small
[cpp11#hydra src]$ ./konzept_small
hallo welt
0 1
255 1
-4100000000 1
256 1 //1
256 0
256 0
[cpp11#hydra src]$
My problem is that:
the result for 256 and higher is wrong. See comment //1
there is no output of the template code on cerr
I started with a version without the cerr, but got only the wrong template result.
I removed a constexpr from the template, but no change.
So I added as last step the cerr to see whats wrong.
Any ideas?
You are not calling is_small<N>, but just printing out its address. You need to change your code to
std::cout << 0 << " " << is_small<0>() << std::endl;
std::cout << 255 << " " <<is_small<255>() << std::endl;
std::cout << -4100000000 << " " << is_small<-4100000000>() << std::endl;
std::cout << 256 << " " << is_small<256>() << std::endl;
Note the added (). Not sure why you are getting the output you are though, are you sure you are running the same code you posted?
is_small is a function you should add the parenthesis :
change
std::cout << 0 << " " << is_small<0> << std::endl;
to this
std::cout << 0 << " " << is_small<0>() << std::endl;
It worked fine for me with this change

GLM multiplication order

I am a little amazed. I have been debugging my code for hours now, and GLM seems to be giving up on me. I am struggling with the following 2 instances:
....
cout << "multiplying A:" << endl;
displayMatrix(node->wMatrix);
cout << "and B:" << endl;
displayMatrix((node->children)[i]->wMatrix);
//switch order!
mat4 temp = (node->children)[i]->wMatrix * node->wMatrix;
cout << "Get result as:" << endl;
displayMatrix(temp);
...
The displayMatrix method is as follows:
void displayMatrix(mat4 &m)
{
cout << m[0][0] << " " << m[0][1] << " " << m[0][2] << " " << m[0][3] << endl;
cout << m[1][0] << " " << m[1][1] << " " << m[1][2] << " " << m[1][3] << endl;
cout << m[2][0] << " " << m[2][1] << " " << m[2][2] << " " << m[2][3] << endl;
cout << m[3][0] << " " << m[3][1] << " " << m[3][2] << " " << m[3][3] << endl;
}
Here is the output I get:
multiplying A:
1 0 0 0
0 1 0 0.5
0 0 1 0
0 0 0 1
and B:
0.540302 -0.841471 0 0
0.841471 0.540302 0 -0.5
0 0 1 0
0 0 0 1
Get result as:
0.540302 -0.841471 0 0
0.841471 0.540302 0 0
0 0 1 0
0 0 0 1
NOTICE that in the code above, the matrix multiplication order is the reverse of what you would write on paper. In other words, the code says B * A. I was very thrown off by this.
The second instance:
cout << "temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "binding matrix inverse: " << endl;
displayMatrix(bindingInvs.at(jIndex));
temp = bindingInvs.at(jIndex) * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "joint world matrix: " << endl;
displayMatrix(joints.at(jIndex)->wMatrix);
temp = (joints.at(jIndex)->wMatrix) * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "weight: " << jWeight << endl;
temp = jWeight * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
The output that I get now is:
temp:
0.087 0 -0.05 1
binding matrix inverse:
1 -0 0 -0
-0 1 -0 0
0 -0 1 -0
-0 0 -0 1
now temp:
0.087 0 -0.05 1
joint world matrix:
1 0 0 0
0 1 0 0.5
0 0 1 0
0 0 0 1
now temp:
0.087 0 -0.05 1
weight: 1
now temp:
0.087 0 -0.05 1
Temp is never getting changed for some reason. I don't know what to do, or why this is happening. My programs compiles and runs (I am pasting from output above). Of course, this is not the entire program. This is only the steps for debugging. But I feel confident that this much should be enough to tell what's going on.
Your displayMatrix function is confusing you, since you print the matrices transposed to what you would expect on paper. GLM uses column major ordering, so the addressing is m[col][row].
Now with that in mind, the operation A*B is actually what you should expect.
For the temp vector, the same problem arises: the first matrix you multiply it by is identity, so it is unchanged. The second matrix is identity, except the last row is 0 0.5 0 1, so x, y and z will be unchanged and the new w' will be 0.5 * y + w. Since y is 0 to begin with, nothing is changed here,too.

Semaphores not waiting on each other

The problem I'm having is that the semaphores are not waiting on each other before the portion of the code is running. The output looks like:
Customer 1 arriving at lane 1 at 0 sec
Customer 1 in now number 1 at lane 1
Checkout1 now serving customer 1 for 10 sec
Customer 2 arriving at lane 2 at 3
Customer 2 in now number 1 at lane 2
Checkout2 now serving customer 2 for 15sec
Customer 3 arriving at lane 1 at 7 sec
Customer 3 in now number 2 at lane 1
Checkout1 now serving customer 3 for 8 sec
Customer 4 arriving at lane 2 at 9
Customer 4 in now number 2 at lane 2
Checkout2 now serving customer 4 for 75sec
Cusomter 1 has left checkout1
Customer 5 arriving at lane 1 at 12 sec
Customer 5 in now number 2 at lane 1
Checkout1 now serving customer 5 for 20 sec
Cusomter 3 has left checkout1
Cusomter 2has left checkout2
Cusomter 5 has left checkout1
Cusomter 4has left checkout2
The problem is that when the checkout1 is processing customer1, the customer is supposed to leave before another person is processed, however, the checkout1 then services another customer which is customer 3. Then near the end of the program, the people start actually leaving the checkouts. I'm pretty sure this is a problem with my semaphores.
Here is a dumbed down version of my code:
sem_t *mem_mutexCheckout1Count;
sem_t *mem_mutexCheckout2Count;
sem_t *mem_mutexCheckout1Line;
sem_t *mem_mutexCheckout2Line;
int *pmemCheckout1Line;
int *pmemCheckout2Line;
int main()
{
for(int i = 0; i < myCustomers.size(); i++)
{
totalArrivalTime += myCustomers[i].arrival;
if((pid = fork()) == 0)
{
InLine(myCustomers[i].serial, totalArrivalTime, myCustomers[i].processing);
_exit(0);
}
}
}
void InLine(int serial, int arrivalTime, int time_interval)
{
sleep(arrivalTime);
if(*pmemCheckout1Line <= *pmemCheckout2Line)
{
cout << "Customer " << serial << " arriving at lane 1 at " << arrivalTime << " sec" << endl;
sem_wait(mem_mutexCheckout1Line);
*pmemCheckout1Line += 1;
sem_post(mem_mutexCheckout1Line);
cout << "Customer " << serial << " in now number " << *pmemCheckout1Line << " at lane 1" << endl;
sem_wait(mem_mutexCheckout1Count);
cout << "Checkout1 now serving customer " << serial << " for " << time_interval << " sec" << endl;
sleep(time_interval);
*pmemCheckout1Line -= 1;
cout << "Cusomter " << serial << " has left checkout1" << endl;
sem_post(mem_mutexCheckout1Count);
}
else
{
cout << "Customer " << serial << " arriving at lane 2 at " << arrivalTime << endl;
sem_wait(mem_mutexCheckout2Line);
*pmemCheckout2Line += 1;
sem_post(mem_mutexCheckout2Line);
cout << "Customer " << serial << " in now number " << *pmemCheckout2Line << " at lane 2" << endl;
sem_wait(mem_mutexCheckout2Count);
cout << "Checkout2 now serving customer " << serial << " for " << time_interval << "sec" << endl;
sleep(time_interval);
*pmemCheckout2Line -= 1;
cout << "Cusomter " << serial << "has left checkout2" << endl;
sem_post(mem_mutexCheckout2Count);
}
}
My myCustomers vector looks like
Vectorindex-Customerserial-timeElapsedSincePrevCustomer-ProcessTime
-------------
[0] 1 0 10
[1] 2 3 15
[2] 3 4 8
[3] 4 2 75
[4] 5 3 20
If you want to prevent any other customer to be processed before the customer who is being processed on the moment leaves, only use one semaphore, which is locked when a customer is being processed and unlocked when the customer is leaving
if(*pmemCheckout1Line <= *pmemCheckout2Line)
{
cout << "Customer " << serial << " arriving at lane 1 at " << arrivalTime << " sec" << endl;
sem_wait(mem_mutexCheckout1Line);
*pmemCheckout1Line += 1;
cout << "Customer " << serial << " in now number " << *pmemCheckout1Line << " at lane 1" << endl;
cout << "Checkout1 now serving customer " << serial << " for " << time_interval << " sec" << endl;
sleep(time_interval);
*pmemCheckout1Line -= 1;
cout << "Cusomter " << serial << " has left checkout1" << endl;
sem_post(mem_mutexCheckout1Line);
}
I found out my semaphores were not in shared memory, hence the semaphores not working properly. I did:
mem_mutexCheckout1Count = (sem_t*) mmap(NULL, sizeof(mem_mutexCheckout1Count), PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
To all my mutex's to fix.

error with io stream

What is the problem with the last two statements in the code?
#include <iostream>
using namespace std;
int main()
{
cout << "2 + 4 = " << 2 + 4 << endl;
cout << "2 * 4 = " << 2 * 4 << endl;
cout << "2 | 4 = " << 2 | 4 << endl;
cout << "2 & 4 = " << 2 & 4 << endl;
What should I do to fix this?
What is the problem with the last two statements in the code?
Operator precedence. | and & have lower precedence than <<, so cout << "2 & 4 = " << 2 & 4 << endl; gets parsed as (cout << "2 & 4 = " << 2) & (4 << endl;).
What should I do to fix this?
Put parens around 2 | 4 and 2 & 4.
Put the expression in parentheses. The << operator is taking precedence over the bitwise operators.
#include <iostream>
using namespace std;
int main()
{
cout << "2 + 4 = " << 2 + 4 << endl;
cout << "2 * 4 = " << 2 * 4 << endl;
cout << "2 | 4 = " << (2 | 4) << endl;
cout << "2 & 4 = " << (2 & 4) << endl;
return 0;
}