What does “state” mean in C++?

https://en.wikipedia.org/wiki/State_(computer_science) might help.

EDIT: The OP was deleted...
Last edited on
Functors can have member variables that can track its state
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
It can’t be a dead horse until we’re at least seven pages into it...
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
Do new texts still talk about functors?
"New" as in post C++11, i.e. from era of lambda closures?
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
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 :/
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).
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:
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.
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
Topic archived. No new replies allowed.