c++ quirks

Talk about anything, including games and servers not affiliated with The Mana World.
Post Reply
User avatar
o11c
Grand Knight
Grand Knight
Posts: 2262
Joined: 20 Feb 2011, 21:09
Location: ^ ^

c++ quirks

Post by o11c »

What is the difference between the following code segments?

Code: Select all

try {
    whatever;
} catch(Foo& foo) {
    throw;
}

Code: Select all

try {
    whatever;
} catch(Foo& foo) {
    throw foo;
}
---

Under what circumstances is if valid to say

Code: Select all

Foo<std::vector>
? Under what circumstances would it be useful? Why wouldn't it be? What are the alternatives?

---

When is it possible to initialize an enum (even a C++0x strict enum!) to a value not contained in the enum?

---

During what parts of a function can you declare variables?

---

What is the main ability of C switch statements that is not in Java?

---

When is the empty statement needed? What are the clearer alternatives?

---

What is the major shortcoming of std::min and std::max?

---

What is the significance of returning a const object by value?
Last edited by o11c on 04 Jul 2011, 20:42, edited 1 time in total.
Former programmer for the TMWA server.
User avatar
Dark_Mag
Warrior
Warrior
Posts: 347
Joined: 18 Dec 2009, 20:14
Contact:

Re: c++ quirks

Post by Dark_Mag »

o11c,

What is this throw for? If it's like return, I know the difference.

Cheers, dark
Image
Image
User avatar
o11c
Grand Knight
Grand Knight
Posts: 2262
Joined: 20 Feb 2011, 21:09
Location: ^ ^

Re: c++ quirks

Post by o11c »

throw; without an expression means "rethrow the exception last caught".

(Also edited to add one more.)
Former programmer for the TMWA server.
User avatar
Dark_Mag
Warrior
Warrior
Posts: 347
Joined: 18 Dec 2009, 20:14
Contact:

Re: c++ quirks

Post by Dark_Mag »

o11c wrote:throw; without an expression means "rethrow the exception last caught".

(Also edited to add one more.)
o11c,

I think lonely "throw" means it'll quit rapidly, and "throw variable" means it'll quit with this variable, for example will print it.
/me thinks, if hes right a bit.

Cheers, dark
Image
Image
User avatar
o11c
Grand Knight
Grand Knight
Posts: 2262
Joined: 20 Feb 2011, 21:09
Location: ^ ^

Re: c++ quirks

Post by o11c »

Dark_Mag wrote:o11c,

I think lonely "throw" means it'll quit rapidly, and "throw variable" means it'll quit with this variable, for example will print it.
/me thinks, if hes right a bit.

Cheers, dark
No.
I know C++ quite well, or I wouldn't be able to make this list.

Throwing an exception does not have any direct correlation to quitting the program (except if an exception is already being thrown, i.e. the second exception comes from a destructor, in which case it will call std::terminate); a thrown exception will unwind the stack until it finds an appropriate catch block.

The function above main() contains such a catch() block, which (on some systems), prints .what() if the exception is descended from std::exception.

The 2 pieces of code in this case are sometimes equivalent, but sometimes not.
Former programmer for the TMWA server.
User avatar
argul
Novice
Novice
Posts: 237
Joined: 08 Aug 2010, 18:43

Re: c++ quirks

Post by argul »

o11c wrote: What is the main ability of C switch statements that is not in Java?
Duffs device... useful for loop unrolling
(Loop unrolling should definitively be benchmarked since instruction cache size matters!)
---
Post Reply