What does “state” mean in C++?

May 14, 2020 at 11:55am
closed account (z05DSL3A)
https://en.wikipedia.org/wiki/State_(computer_science) might help.

EDIT: The OP was deleted...
Last edited on May 16, 2020 at 5:24pm
May 15, 2020 at 2:05pm
Functors can have member variables that can track its state
May 15, 2020 at 3:28pm
Flogging our dead horse, in practical terms, an object's "state" could be considered its internal variables, and the values those variables have. Functors, being function objects, can have variables which can have values, and those values (i.e. the state) can even persist between uses of the functor.
Last edited on May 15, 2020 at 3:28pm
May 15, 2020 at 7:47pm
It can’t be a dead horse until we’re at least seven pages into it...
May 15, 2020 at 8:20pm
Let's keep it going.

A pure function is usually defined on two key conditions:
1. The same input will always produce the same output.
2. The function has no side-effects that change some external state. (Although, I've heard some people suggest that a function can still be pure if it modifies its input arguments...)

For example, mathematical functions are the easiest to understand here. sin(x) will always produce the same result for some input value of x. However, a function like get_current_time() is not pure, because it depends on some state external to the function's input itself -- in this case, the current system's time. Calling the function at 3:00 will produce a different result than calling the function at 11:00. But sin(pi) will always be 0.

C++ is not a functional programming language, so you can easily have state in normal functions through use of global variables or static variables. A "functor" in C++ is basically just a class where you overload the () operator to act like a function. Actual functional programming languages like Haskell have a much more mathematical meaning that I don't really understand :)
Last edited on May 15, 2020 at 8:27pm
May 15, 2020 at 10:14pm
Do new texts still talk about functors?
"New" as in post C++11, i.e. from era of lambda closures?
May 15, 2020 at 11:26pm
I've had to use functors in otherwise post-C++11 code. It was kernel-mode code so I was fairly restricted in what I could do. Don't dismiss functors just because there are newer alternatives.
Last edited on May 15, 2020 at 11:26pm
May 16, 2020 at 9:14am
Do new texts still talk about functors?


New texts still talk about raw arrays and manual memory management as the first choice, in the first chapters; the teaching of C++ will take a long time to catch up to the C++11 era :/
May 16, 2020 at 2:22pm
Lets rephrase: Have the new texts added lambdas to the functor section?

I seem to remember section "Functors, Functor Classes, Functions, etc." (Items 38-42) of Scott Meyers' "Effective STL" (2001).
May 16, 2020 at 2:34pm
Do new texts still talk about functors?

Yes, though it depends on the author(s) and the intent of the text.

I have several books post-C++11 that talk about functors. One IIRC even explains lambdas by way of previously explained functors.

Same with SO, one commenter saying a lambda is a nameless functor:
https://stackoverflow.com/questions/4686507/lambda-expression-vs-functor-in-c
May 16, 2020 at 2:42pm
New texts still talk about raw arrays and manual memory management as the first choice

New texts also use C functions such as srand/rand as the "go-to" means to generate random numbers and MAYBE later (much later) briefly introduce <random>/<chrono>.

Same with C strings. C++ std::string is shoehorned in as an afterthought.

Using iterators to traverse a C++ container instead of operator[] indexing? HA!

Nearly all of "modern" C++ I had to discover on my own. Which isn't such a bad thing, honestly.

Most post-C++11 books that do a decent job of explaining the language changed are more reference material than "learn C++" books.
May 16, 2020 at 2:46pm
Oh, cool, the OP is a "change the text so we can add a spam link" spammer.

Looks like someone else spotted the spam and took care of it.
Last edited on May 16, 2020 at 2:49pm
Topic archived. No new replies allowed.