[comp.lang.c++] Cast to void

david@cs.washington.edu (David Callahan) (02/16/90)

Suppose that you had a parallel program with a shared object (like
ostream cout) such that you wanted certain expressions to be performed
atomically. For example, you wanted the line
	cout << file << ":" << line << ":" << msg << "\n";
to be continguous but you don't care its relative position with regard
to similar messages

A scheme I came up with was to differentiate the first operator from
the last in the type system and then catch the final expression as a
cast to void. Here was the test program. "foo" plays the role of ostream
and "foo1" is introduced to handle "interior" operators. 

--------------------------------------------------
class foo1 {
    protected:
      void lock() ;      
      void unlock() ;      
    public:
     operator void() ; 
     struct foo1 & operator << (int) ;
} ;

struct foo : public foo1 {
      foo1 & operator << (int i) ;
} ;

main () {

      foo f ;
      int i ;
      f << i << i ;
      void(f << i << i) ;
      (void) (f << i << i) ;
}

foo1 & foo::operator << (int i) {
      lock() ;
      return foo1::operator<<(i) ;
}

foo1::operator void() { unlock() ; }
--------------------------------------------------

I had hoped that the line
	f << i << i ;
would be implemented as:
	foo1::void(
            foo1::operaotr<< (foo::operator<<(&f,i)
		              i)) 
but alas g++ (gcc version 1.36.1 (based on GCC 1.36)) would not make
the call to operator void, even when the cast to void was made
explicit as on the last two lines.

Is defining operator void legal? (g++ gave no messages) If so, it
seems that g++ is in error when the explicit cast is present.
Should implicit casts to void also invoke the operator void? I
certainly hope so since explicit casts to void seem very uncommon.

-- 
David Callahan  (david@tera.com, david@june.cs.washington.edu,david@rice.edu)
Tera Computer Co. 	400 North 34th Street  		Seattle WA, 98103