Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Simulating ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 9498 of 9831
Post > Topic >>

Re: Simulating new user-defined operators

by Alberto Ganesh Barbati <AlbertoBarbati@[EMAIL PROTECTED] > Apr 15, 2008 at 11:19 PM

Michael Aaron Safyan ha scritto:
> 
> For the second example, functions should throw exceptions, by default.
> If you don't want a function to throw exceptions, then they should be
> explicitly disabled as in the following:
> 
> disable_exceptions{
>    // code which normally throws exceptions ceases to throw exceptions
> }
> 

I really don't see the relation****p with the OP's example. Moreover, it
seems to me that it's a bad idea in the first place: if a function is
designed to throw an exception to return an error condition and you
disallow that, what should the function do to return the error
condition? If the function calls a library function which is not under
your control, how can you disallow exception thrown by the library
function? In the end, it's much safer to wrap the code in a try/catch
block rather than using your approach.

> The above can be implemented with something along the lines of:
> 
> class ExceptionDisabler
> {
>     public:
>         ExceptionDisabler() : _test(true) {
>             ExceptionManager::push(false); // disable
>         }
>         ~ExceptionDisabler(){
>             ExceptionManager::pop();       // restore
>         }
> 
>         void operator++(){ _test = false; }
>         void operator++(int){ _test = false; }
>         operator bool()const{ return _test; }
>     private:
>         bool _test;
> };
> 
> #define TOKENCAT(X,Y) TOKENCAT2(X,Y)
> #define TOKENCAT2(X,Y) X ## Y
> 
> #define disable_exceptions                     \
> for ( ExceptionDisabler TOKENCAT(ed,__LINE__); \
>       TOKENCAT(ed,__LINE__);                   \
>       TOKENCAT(ed,__LINE__)++ )                \
> 

BTW, there is simpler a way of implementing this that doesn't require a
loop:

class ExceptionDisabler
{
     public:
         ExceptionDisabler(int) {
             ExceptionManager::push(false); // disable
         }
         ~ExceptionDisabler() {
             ExceptionManager::pop();       // restore
         }

         operator bool() const { return false; }
};

#define TOKENCAT(X,Y) TOKENCAT2(X,Y)
#define TOKENCAT2(X,Y) X ## Y

#define disable_exceptions \
   if (ExceptionDisabler TOKENCAT(ed,__LINE__) = 0) {} else

HTH,

Ganesh

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 11 Posts in Topic:
Simulating new user-defined operators
Vidar Hasfjord <vattil  2008-04-14 12:43:32 
Re: Simulating new user-defined operators
Michael Aaron Safyan <  2008-04-14 21:25:12 
Re: Simulating new user-defined operators
"Martin T." <  2008-04-15 12:28:37 
Re: Simulating new user-defined operators
Edward Rosten <Edward.  2008-04-15 12:33:04 
Re: Simulating new user-defined operators
Alberto Ganesh Barbati &l  2008-04-15 23:19:14 
Re: Simulating new user-defined operators
Seungbeom Kim <musiphi  2008-04-15 23:18:03 
Re: Simulating new user-defined operators
ManicQin <ManicQin@[EM  2008-04-16 03:58:47 
Re: Simulating new user-defined operators
Vidar Hasfjord <vattil  2008-04-16 03:55:06 
Re: Simulating new user-defined operators
Carl Barron <cbarron41  2008-04-16 03:59:56 
Re: Simulating new user-defined operators
Michael Aaron Safyan <  2008-04-16 04:13:19 
Re: Simulating new user-defined operators
Vidar Hasfjord <vattil  2008-04-16 04:13:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:50:03 CDT 2008.