Functional Programming in C++ by Ivan Čukić

Functional Programming in C++ by Ivan Čukić

Author:Ivan Čukić
Language: eng
Format: epub, pdf
ISBN: 9781617293818
Published: 2018-10-26T16:00:00+00:00


② …but the output is "Smith, John".

The problem is that you’re now storing references to the strings you need to concatenate. If the strings change between the point where you created the expression (the declaration of the fullname variable) and the point where you’re required to compute the result (writing fullname to the standard output), you’ll get an unexpected result. Changes to the strings made after you think you concatenated them will be reflected in the result string.

This is an important thing to keep in mind: in order to work as expected, laziness requires purity. Pure functions give you the same result whenever you call them with the same arguments. And that’s why you can delay executing them without any consequences. As soon as you allow side effects such as changing a value of a variable to influence the result of your operation, you’ll get undesired results when trying to make it execute lazily.

Expression templates let you generate structures that represent a computation instead of immediately calculating that expression. This way, you can choose when the computation will take place; you can change the normal order in which C++ evaluates expressions and transform the expressions any way you want.2

2 If you need to build and transform more-complex expression trees, check out the Boost.Proto library at http://mng.bz/pEqP.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.