Page 1 of 1

c++ quirks

Posted: 04 Jul 2011, 20:22
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?

Re: c++ quirks

Posted: 04 Jul 2011, 20:24
by Dark_Mag
o11c,

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

Cheers, dark

Re: c++ quirks

Posted: 04 Jul 2011, 20:43
by o11c
throw; without an expression means "rethrow the exception last caught".

(Also edited to add one more.)

Re: c++ quirks

Posted: 04 Jul 2011, 20:49
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

Re: c++ quirks

Posted: 04 Jul 2011, 22:14
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.

Re: c++ quirks

Posted: 05 Jul 2011, 18:02
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!)