What does a semicolon after function inside function mean? - c++

I saw an answer for a c++ challenge that had you copy a certain part of a string x times.
std::string repeatString(int xTimes)
{
repeatString;(3); //What's happening here?
}
He seemed to have solved the challenge with this code and I'm guessing he solved it the wrong way but I'm still unsure what's happening.
original challenge:
https://edabit.com/challenge/vxpP4nnDhRr2Yc3Lo
original answer by Marcus_2008

repeatString; is an expression that does nothing useful. Same for (3);. Its the same as 3; and does literally nothing. After removing that unnecessary fluff, the function is
std::string repeatString(int) {
//What's happening here?
// - nothing at all
}
And this, not only does it not repeat a string, but it invokes undefined behavior when called, because it is declared to return a std::string but does not.
Even changing the line to repeatString(3); would leave us with the same issue while return repeatString(3); would result in infinite recursion.
It is not possible that this code solves the task. You must have misunderstood something, or the real code looks different.

Related

IEntity* wLocalEntity pointer example code snipet

what does this C++ code snippet do?
IEntity* wLocalEntity= const_cast<IEntity*>(BaseSimSystem::getEntityRef());
if(wLocalEntity!=0){
mEntitySpeed=wLocalEntity->getSpeed();
}
I'm not sure how it's related to a template creation. Can someone explain to me what this code does?
Thank you.
Here's the code.
IEntity* wLocalEntity= const_cast<IEntity*>(BaseSimSystem::getEntityRef());
if(wLocalEntity!=0){
mEntitySpeed=wLocalEntity->getSpeed();
}
If you actually get asked about this code, the first thing I'd do is complain about the if-clause. that 0 should be nullptr as so:
if (wLocalEntity != nullptr) {
mEntitySpeed = wLocalEntity->getSpeed();
}
Also, please tell me you know you shouldn't compress your code so tightly. Bugs hide when you shove all those operators together with no whitespace.
Now, let's look at line 1:
IEntity* wLocalEntity = const_cast<IEntity*>(BaseSimSystem::getEntityRef());
Clearly, wLocalEntity is a pointer to an IEntity. I hope you understood that.
The const_cast<> bit is ridiculous and possibly a bug. We don't know what BaseSimSystem::getEntityRef() returns, but I suspect it returns a const pointer, and now you're trying to assign that back to a non-const variable. The const_cast<> is getting rid of the constness.
The correct code is almost certainly:
const IEntity * wLocalEntity = BaseSimSystem::getEntityRef();
However, it's possible that IEntity has methods that aren't flag const that really should be, so you might have to do this because some other programmer didn't apply const when he should have.
So the const_cast<IEntity *> says "take the return value in those parenthesis and yes, I know they're not a non-const IEntity *, but don't warn me about it because supposedly I know what I'm doing.
How's that?

While Loops in C++ without braces and a function call in parenthesis?

In C++ a while statement like this...
while (currGame.playGame(currPlayer) == true);
is perfectly legal as long as the playGame function returns true or false but is this considered ugly code?
Well, it's not pretty. If a null statement is used as the loop body, it's better to make it explicit:
while (currGame.playGame(currPlayer))
; // do nothing
Or, better:
while (currGame.playGame(currPlayer)) {
}
I've also removed == true part as it's not a good coding style, either.
It can be simplified to
while (currGame.playGame(currPlayer));
This kind of while loops is rare. It is usually in a form like this:
while (currGame.hasNext())
{
currGame.playGame(currPlayer);
}
Ugly or pretty depend on you! But keep in mind
Programs should be written for people to read, and only incidentally for machines to execute.
Make it clearer to both humans and compilers that the code does
what you intended.
As Other answers I would shorten your code to
while (currGame.playGame(currPlayer));. But anyway this is not a big deal.
For further this small book would be a good reference to read http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586
while (currGame.playGame(currPlayer) == true);
is perfectly legal but is less readable. A more explicit way of doing the same is :
while (currGame.playGame(currPlayer))
;; // do nothing
/* A single semicolon is just fine as mentioned by #yu-hao
* but a lot of C/C++ libraries use ;; for an empty statement.
*/
or
while (currGame.playGame(currPlayer)){
;; // do nothing
}
Why should you avoid while(condition);
In the below example :
while(true) // suppose you missed a semicolon here
return 0; // Instead of expected infinite loop, your program exits.
Notes
Avoid == true stuff because it can easily be mistakenly typed as = true - say you had a key-jump - and if that is the case, you have a bug which is difficult to catch.

Information about piece of code concerning Brackets and Templates

This is a part of the code:
tmp<scalarField> nu(const label patchi) const
{
return nu_.boundaryField()[patchi];
}
I don't really understand the meaning of brackets after functions parentheses. Is this correct syntax and what does it actually mean?
Second question would be about this "tmp". Is that standart syntax of writing temptates or one can choose everything and write for example hallo.scalarField> or example.scalarField>.
Thanks in Advance.
Provided boundaryField() returns something that supports [] syntax, that is valid. For example, if foo is a function that returns a reference to an array, foo()[0] would get the first element of said array.

Instructions after return statement in a C++ function

I've seen this C++ function in a ROS kobuki node (kobuki-auto-docking). I wonder if the while is dead code ? Or if it can be called for a misterious reason sometimes ?
void AutoDockingROS::spin()
{
return;
while(!shutdown_requested_){;}
}
Thanks for your helps,
In C++, nothing after return is executed.
But, you should always respect the code you see in front of you:
If the preprocessor #defines return to something else for a particularly odd build configuration then the code could run.
Someone might be porting the code blindly to Java. In Java, code within a finally block does run after a return.
It's possible the developer retained the line to test the syntactic validity of !shutdown_requested_
All unlikely scenarios (I have seen the first one in production by the way) but worth checking if you're going to undertake a large refactoring effort.
Everything after a return statement is never going to be executed.
It is equivalent to this:
void AutoDockingROS::spin()
{
return;
// while(!shutdown_requested_){;}
}
Effectively, the programmer wanted to leave some code there, perhaps as a reminder. It is never executed.
No one can call code after the return... Well maybe only Chuck Norris can...
Nothing can be executed after the return statement until its within a condition. Put the code above the return statement.

Infix Expressions

I am trying to evaluate an infix expression using two stacks, however, my program keeps getting a segmentation fault and I am not sure what is causing the error. I have tried following the pseudocode for the RPN algorithm, however I think my issue arises when I call doOperation. I am not sure what parameters to include when calling this. I know I need a (ValueType, char, ValueType), however I do not want to write doOperation (ch, ch, ch) since I am pretty sure that won't help. Can anyone help me figure out a way to call this function? (I'm pretty sure that's one of the reasons causing the segmentation fault).
The opStack and valStack in doOperation should use the variable in processExpression.
Its function prototype should be like this:
ValueType doOperation(ValueType operandL, char operation, ValueType operandR, stack<char>& opStack, stack<ValueType>& valueStack)
Pay attention to the last two parameters: stack<char>& opStack, stack<ValueType>& valueStack. They must be pointer-passed or reference-passed, NOT value-passed.
processExpression call doOperation like this: doOperation(operandL, ch, operandR, opStack, valueStack) .
Besides, the current segmentation fault happens because opStack and valueStack defined in doOperation has no items. top() will reference noexist value.
Try removing these lines from doOperation:
stack<char> opStack;
stack<ValueType> valStack;
operandR = valStack.top();
valStack.pop();
operandL = valStack.top();
valStack.pop();
operation = opStack.top();
opStack.pop();
Note that your declaration of double result isn't there - you should keep that.
So, what's happening in the above lines:
You create stacks opStack and valStack. Both of these are empty.
You call .top(), which does bad things when the stack is empty. .pop() does bad things on empty stacks as well.
You are attempting to assign values to the parameters you passed in. Even if this was successful, your parameters would be useless. You just end up creating/initializing them in your function any way.
Now, after removing the above lines, you'll need to change your calls to doOperation. In processExpression you will want to do these calls before calling doOperation:
operandL = valStack.top();
valStack.pop();
operandR = valStack.top();
valStack.pop();
operation = opStack.top();
opStack.pop();
doOperation(operandL, operation, operandR)
Which isn't pretty, especially when you do that for the three times you call doOperation, but it's a start. The first goal is to get working code. You can make it pretty if you're so inclined later.
Also, and this is a bit pedantic, but you should rename your operation variable to be operator, since that is what it really is. The "operation" is the thing that happens when you execute the operator.