10

On page 57 of The Design and Evolution of C++, Dr. Stroustrup talks about a feature that was initially part of C with Classes, but it isn't part of modern C++(standard C++). The feature is called call/return. This is an example:

class myclass
{
  call() { /* do something before each call to a function. */ }
  return() { /* do something else after each call to a function. */ }
  ...
};

I find this feature very interesting. Does any modern language have this particular feature?

FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • 4
    Looks similar to AOP: http://en.wikipedia.org/wiki/Aspect-oriented_programming – Georg Fritzsche Apr 22 '10 at 03:32
  • @gf That's interesting, I think I am going to take a look at some AOP framework for C++, because I have never done that before :) – Khaled Alshaya Apr 22 '10 at 03:41
  • This looks very aspect-oriented. I'm curious why this was considered and then dropped. Also... I don't have that book so I have to ask: was this feature designed to apply before *any* function of the class, or before a *specific* function? Would it have been possible to define different `call()` s and `return()` s for different functions? – FrustratedWithFormsDesigner Apr 22 '10 at 03:43
  • 2
    @FrustratedWithFormsDesigner Dr. Stroustrup said that it was removed because he was the only one who used that feature in "The Task Library' which he was writing, and he "completely failed" to convince people that it had an important use. – Khaled Alshaya Apr 22 '10 at 03:47
  • 1
    Too bad, it sounds like he was a bit ahead of the times with this one. – FrustratedWithFormsDesigner Apr 22 '10 at 04:11

3 Answers3

7

The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor implementing call(), and upon return (or abnormal exit), its destructor implements return().

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Are not sentry objects just for input and output streams ? – Romain Hippeau Apr 22 '10 at 03:51
  • +1 Thanks, I didn't even know about this class in C++. Hopefully I'll try to implement my own! – Khaled Alshaya Apr 22 '10 at 03:56
  • @Romain: you can make sentry objects for anything you want. I use them frequently while debugging, and to ensure invariants are maintained. They're also frequently employed to help ensure functions are properly exception safe. – Dennis Zickefoose Apr 22 '10 at 04:07
  • 2
    Sentry objects are very similar indeed. On the one hand they require explicit instantiation (and being passed `this`) but on the other hand you can add to them so that they check not only the invariants of the class but some pre/post conditions for the function at hand. – Matthieu M. Apr 22 '10 at 06:21
  • This is not really a language construct, but an idiom – Romain Hippeau Apr 23 '10 at 01:45
  • @Romain: correct. When you implement it, though, it works perfectly. – Potatoswatter Apr 23 '10 at 01:47
2

Aspect Oriented Programming has this. http://en.wikipedia.org/wiki/Aspect-oriented_programming

Aspect Oriented Programming (also known as AOP) has the ability to create interceptors before, after and around code.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
0

The D2 programming language has this and more with it's ScopeGuards. It's designed so you can use multiple ones, they work like a FILO stack.

Felan
  • 1,273
  • 10
  • 17