[antlr-interest] "finally" blocks

Terence Parr parrt at cs.usfca.edu
Mon Jan 17 15:23:29 PST 2005


On Jan 17, 2005, at 3:13 PM, Paul J. Lucas wrote:

> On Mon, 17 Jan 2005, Christopher Schultz wrote:
>
>> I think that's a flawed comparison. Destructors are in C++ what 
>> finalizers
>> are in Java.  A "finally" block is different.
>
> 	Yes, but I can use destructors in C++ to implement "finally"
> 	blocks.
>
> 		class bool_flag {
> 		public:
> 		   bool_flag( bool &b ) : m_b( b ) {
> 		       m_b = true;
> 		   }
> 		   ~bool_flag() {
> 		       m_b = false;
> 		   }
> 		private:
> 		   bool &m_b;
> 		};
>
> 		bool my_flag;
>
> 		void f() {
> 		    bool_flag( my_flag );
> 		    // ...
> 		}
>
> 	No matter how f() exits, the "finally" of setting my_flag to
> 	false will always be done.  Hence, destructors can be used to
> 	implement "finally" (that's why C++ doesn't need "finally"
> 	explicitly) but Java having "finally" can't get you
> 	destructors.

Interesting...was wondering how to do that in C++.  I wonder if it's 
general though.  A finally clause can play with all sorts of local 
variables and call methods etc...  I wonder if you'd need a whole bunch 
of these classes.  I guess so.  Would ANTLR need to analyze C++ actions 
(shudder) in order to construct the bool_flag(arglist) thing?  
Probably.

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!





More information about the antlr-interest mailing list