6 ms·
One of his definitions of a pure function is one that only has by value parameters and doesn't change state. Am I correct in thinking that in C++, the advent of
by hellofunk 10y ago
One of his definitions of a pure function is one that only has by value parameters and doesn't change state. Am I correct in thinking that in C++, the advent of C++11 lambdas allows you to be explicit about this and prevent the compiler from allowing you to accidentally use variables from outside the scope of the function's parameters, by writing lambadas (named, if necessary, like a normal function) with no-capture lists ("[]") which would force you to work in a more pure style. In C++, what other method might help you enforce purity?
- pbsd 10y agoYou can still freely access global variables inside lambdas, with or without captures.
- hellofunk 10y agoThanks for clarification. I thought the capture list was required for this. I suppose the capture list is for capturing variables that are local to the scope of the lambda's definition.
- hellofunk 10y agoI have been testing this, and I'm not convinced. A lambda with no capture at all (just []) will not compile if it accesses any variable in the scope of the lambda definition (including globals). I must add [=] or similar to get access to them. What were you referring to? The compiler error is clear: "variable 'lam' cannot be implicitly captured in a lambda with no capture-default specified"