Information about piece of code concerning Brackets and Templates - c++

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.

Related

Why is my private member displayed as another type than it is defined as?

I'm currently working on a project in C++ and I'm just not allowed to push_back on my vector (compile error).
The method where everything seems to go wrong looks like this:
DetectionResult DetectionManager::Update(DetectionInput& input) const
{
std::vector<DetectionResultUnfiltered> results;
results.reserve(m_detectionModels.size());
for (auto& detectionModel : m_detectionModels)
(
std::future<void> future = std::async(std::launch::async, UpdateDetectionModelAsynchronously, detectionModel, &results, &input);
m_futures.push_back(future); // <-- Compile error only on this line
)
}
I think it is rather unimportant what exactly the other called method does and how those types are structured exactly. The only important thing should be that the field m_futures is of the type std::vector<std::future<void>>.
Even when hovering the m_futures in Visual Studio within that method, it clearly shows me that it is of the correct type (field) std::vector<std::future<void>> DetectionManager::m_futures.
But still the .push_back() call is underlined in red, and when hovered it shows the following error: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::future<void>, _Alloc=std::allocator<std::future<void>>]" matches the argument list and object (the object has type qualifiers that prevent a match) - argument types are: (std::future<void>) - object type is: const std::vector<std::future<void>, std::allocator<std::future<void>>>
I'm pretty sure my vector is not really handled as a vector in this current case, because when auto completing the method calls on my vector I don't get even a suggestion for .push_back() or .emplace_back() or something like this. I think it is handled as an object of a type I imported from another library (opencv::mat or something like that), because at some point it was even shown like that when hovered.
And by the way,
the method call is not shown as an error when I do this:
(static_cast<std::vector<std::future<void>>>(m_futures)).push_back(future);
so by explicitly casting it to a vector it seems to work again.
I just don't know exactly what happens here. I've traced down and commented some includes to make sure nothing weird was included. And while doing that I figured out that I don't get any errors highlighted within the DetectionManager.h even when commenting out the #include (Even if not including ANYTHING in the .h at all, only the #include is needed when working with that type).
It doesn't even work on other vectors as well, so when implementing an example vector which only holds bools, I don't need to include the and everything looks right according to Visual Studio, which is weird enough since I didn't include or which I'm using as well.
Does anyone have any idea what it might be? Or how to track down this error?
Big thanks in advance!
As Borgleader correctly pointed out, the method was marked as const - but when pushing back, I am modifying the class field which is obviously not allowed.
The const was a leftover from an older design I just reworked and thus I completely forgot about it.
That paired with some other weird errors I had before (like the field being shown as a complete different type from some library I imported even though it was clearly declared as a vector) didn't help.
I still don't quite understand why my project is compiling when not including or within the header file at all even when I declare fields with those types, but at least that's not stopping me from building and running the program.

What does a semicolon after function inside function mean?

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.

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?

condition.wait_for intellisense error

I'm doing a simple tutorial based around threads. In this exercise I am supposed to get threads to wait for each other.
I have copied the example code verbatim with the exception that I'm not using namespace std; and am instead writing in std::each time I need it.
The error in question corresponds to the line:
if (condition.wait_for(std::unique_lock<std::mutex>(mut), std::chrono::seconds(3)))
Intellisense tells me that "the expression must have bool type (or be convertible to bool)" but I looked up the documentation on condition.wait_for and it can return std::cv_status::timeout, std::cv_status::no_timeout as well as true and false. When I go to build, it thinks it can only return std::cv_status.
It should work right? Does it require the third parameter? The example I'm following doesn't use one.
As Bo says, there are 2 versions of the function. I'm going to assume there was a typo in the example and it meant to use the version of the function with three parameters, here's some working code, I don't know if it works in the same way as the example intended.
if (condition.wait_for(std::unique_lock<std::mutex>(mut), std::chrono::seconds(3), [] {return true; }))

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.