views:

210

answers:

5

I recently tried to understand a C++ program that was written by somebody who I assume had a background in functional programming: For example, he had declared a closure class which he extensively used and which does somewhat resemble what is known as a closure in functional programming. Another class was called a guard, but I haven't quite figured out yet what it is good for. It seems to have some sort of cleanup functionality attached to it.

The only language in which I have seen a concept called guard is Erlang, but that does not remotely look similar to the code I found. In what other languages does such a concept exist that the author of the C++ code may have alluded to?

+3  A: 

Haskell's Control.Monad module has guard:

guard :: MonadPlus m => Bool -> m ()
guard b is return () if b is True, and mzero if b is False.

For example, to compute Pythagorean triples where each leg is no longer than 25, you could use

triples = do
  a <- [1..25]
  b <- [a..25]
  c <- [b..25]
  guard (p a b c)
  return (a,b,c)
  where
    p a b c = a*a + b*b == c*c

For an explanation of what's going on, see my blog post A programmable semicolon explained.

Greg Bacon
+3  A: 

In general terms, a guard is simply a construct which needs to evaluate to true for execution along some path to continue. This or something like it exists in all useful Turing-complete programming languages, but is perhaps so basic that is often not named separately as an entity. Here's a simple example in Haskell:

f x
 | x < 0 = -x
 | otherwise = x

This is equivalent to the absolute value function: negate a number if it's negative to produce its positive counterpart; otherwise, return the same value passed in. There are two guards here: x < 0, which is true when x is less than zero, and otherwise, which is always true.

John Feminella
+1  A: 

Guards in computer science typically refer to the Boolean expression that indicates that a looping construct should continue. For example (pardon the pun)

for (int i = 0; i < N; ++i)
  /* stuff */

Here, i < N is the guard.

It's difficult to answer your question more thoroughly without more information.

Peter Alexander
+5  A: 

The term "guard" is used in several functional languages the way it is used in Erlang, but that usage doesn't seem to fit your description. Without seeing the C++ code it's hard to really know what was intended by it.

A guess by your description would be that it implements something like Haskell's bracket, which basically ensures that some resources are released if the wrapped function exits, even if that happened by an exception. In Python one would use finally for this, in C++ you usually have the cleanup code in the destructor of an object on the stack.

sth
+5  A: 

To me it sounds like he was using RAII.
The class constructor/destructor is used to symetrically handle some form of resource allocation/release in an exception safe context (What Java programmers would call finally {} as the destructor is guranteed to be called.).

This is a very common C++ idiom and is ued extensively in modern C++.

Did the code look like this:

void Plop()
{
     Guard   guard(lock);
     // Do lots of stuff
}

Here the guard is locking the lock in the constructor and unlocking the lock in the destructor.

Martin York
+1 I suspect this is the actual correct answer. The functional stuff is probably a red herring.
Logan Capaldo
If you mean "Resource Allocation Is Initialization" you are almost certainly right. Thanks! I'm still not quite sure what the word `guard` alludes to, though.
hillu
It guards against exceptions causing problems!
Martin York