• (2 Pages)
  • +
  • 1
  • 2

do {} while(0); Rate Topic: ***-- 2 Votes

#16 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Posted 19 August 2011 - 07:36 AM

Are you saying you would use exceptions as a replacement for the usual flow control statements?
Was This Post Helpful? 0
  • +
  • -

#17 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4498
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Posted 19 August 2011 - 11:18 AM

I can't speak for C++ exceptions. I know that Java and .NET exceptions are a bit on the heavy side, and you're simply not supposed to use them for program flow. They're for...well, exactly what they say they are. Exceptional circumstances.
Was This Post Helpful? 0
  • +
  • -

#18 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Posted 19 August 2011 - 02:59 PM

I believe they are much faster in C++. I've hard people complaining about how slow Java exceptions are after switching from C++. Of course, it doesn't matter if you use them for what they are intended but if they are your standard way of returning from leaf nodes in a tree traversal, it's going to have much more of an impact!
Was This Post Helpful? 0
  • +
  • -

#19 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Posted 20 August 2011 - 12:10 AM

View Postcfoley, on 20 August 2011 - 01:36 AM, said:

Are you saying you would use exceptions as a replacement for the usual flow control statements?

No, I'm saying I'd use exceptions as a replacement for error handling for both do {} while (0) and return.
Was This Post Helpful? 1
  • +
  • -

#20 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Posted 20 August 2011 - 02:11 PM

i think people are looking at this wrong. no you shouldn't use exceptions for control flow but they are implemented using control flow hence to emulate them you must use control flow. this is actually a neat trick for C but not C++. some people have said "call it what it is" and use a goto statement; well you have a point but i would say this is in someways better than goto. goto has no scope, it can jump into the middle of another function(very very bad). this little hack can't do that however so it's a bit safer. still i would avoid this very strongly just as using goto's should be avoided. there are some instances however(like were Linux uses them in it's networking code) that this kind of thing could be ok.

This post has been edited by ishkabible: 20 August 2011 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#21 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Posted 23 August 2011 - 09:57 AM

I would not use the single iteration loop or the goto's. I might use a switch statement:

    switch(0) {
        case 0:
            p=createObject1();
            if (!p) {  break; }
        case 1:
            o=createObject2();
            if (!o) { 
                delete p;
                break; 
            }     
        case 2:
        default:
        break;
    }


However there is a far better chance that I would just use nested blocks.

Quote

Can you explain why they'd be useful in macros?


They are useful in macros for creating "function-like" macros in C to create a scope to define temporary variables under. Of course in C++ an inline function is much better than a do { } while(0); macro AND if you don't need to simulate multiple return statements (i.e. break statements in the do-while) then you can just use a block
{ ... }
to create a temporary scope. So... in all honestly I don't think a do-while(0) is really a terribly useful construct to have.

However I don't know if it is a terribly BAD thing -- its just a pattern and one that might be "just the thing" for some problem one day.
Was This Post Helpful? 0
  • +
  • -

#22 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Posted 03 September 2011 - 07:30 PM

View PostCurtis Rutland, on 18 August 2011 - 06:55 PM, said:

Can you explain why they'd be useful in macros?


Sorry for the late response.

do { something; } while (false) forces a semicolon after the macro invocation, no other token is allowed to follow.
Was This Post Helpful? 1
  • +
  • -

#23 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Posted 12 September 2011 - 05:22 AM

I see no use for "do { } while(0);" other than in a macro and if you're using a macro it should be only as a last resort.
Was This Post Helpful? 0
  • +
  • -

#24 impopia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-March 12

Posted 10 March 2012 - 03:28 PM

Quite an argument here about some twisted way of coding (or obfuscating ?...). Of course nested blocks or exceptions are the methods of choice.
That piece of code started with CreateNewObject(), then if this is well done, create another new object etc. But every creation should have a corresponding destruction. What if the second creation is unsuccessful and the first object created is now of no use ? It must be destroyed, and this is done with nested blocks. Or exceptions... So the code of UziTech would look like this:
1	p = createnewobject();
2	if(p != 0)
3	{
4	     o = createdifferentobject();
5	     if(o != 0)
6	     {
7	          //...
8	     }
	     [color="#FF0000"]else DestroyObject(p);[/color]
9	}


Was This Post Helpful? 0
  • +
  • -

#25 karagulamos  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 17
  • Joined: 24-November 10

Posted 12 August 2012 - 03:10 PM

View Postsparkart, on 17 August 2011 - 11:14 PM, said:

With the do while loop, you can break out of the code on failure:

do
{
  // Failure with return a null(0) value.
  p = CreateNewObject();
  if (p == 0) break;

  o = CreateDifferentObject();
  if (o == 0) break;

  //...
} while(0);



The above method can be avoided. Assuming that the code above is legally correct and the intention is to "do stuff" only when the creation of objects p && o succeeds, then I find the following examples more intuitive:

Example 1
=========


//...
p = CreatNewObject();
o = CreateDifferentObject(); 

if(p != 0 && o != 0){ // true if the creation of p && o succeeded
   //do stuff ...
}

// Perhaps some more code and/or cleanup ...



Note that Example 1 incurs the extra cost of creating the object o regardless of whether or not the creation of p succeeded.

Example 2
=========


bool created = false;

//...
p = CreateNewObject();

if(p != 0){
   o = CreateDifferentObject();
   if(o != 0) created = true;
}

if(created){ // true if the creation of p && o succeeded
   //do stuff ...
}

// Perhaps some more code and/or cleanup ...



Cheers!

This post has been edited by karagulamos: 12 August 2012 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2