exception from C function?

This is a discussion on exception from C function? within the C++ Programming forums, part of the General Programming Boards category; Hi Mats,
I little confused even if I agree with you. Looks like we are talking about template function will ...

I little confused even if I agree with you. Looks like we are talking about template function will degrade performance compared with pure C implementation of qsort(), but you are talking template function could improve performance by inline some code like compare for POD types?

Originally Posted by matsp

By inlining the compare operation - the templated sort function just uses the < operator, so if it's an integer compare, it will just do that. If it's an inlinable operator< in the class object, then the compiler can inline it.

Calling a function is always overhead compared to just doing the same work "here". The amount of overhead is depending on the amount of work in the function. Since qsort() may do A LOT of calls to the compare function, and comparing two data-items is USUALLY trivial, the overhead can be quite noticeable.

qsort itself may be inlined, but the comparison will ALWAYS be called through a function pointer.

I little confused even if I agree with you. Looks like we are talking about template function will degrade performance compared with pure C implementation of qsort(), but you are talking template function could improve performance by inline some code like compare for POD types?

regards,
George

Template functions will normally be faster than qsort - that is the whole point of the last 7 or so posts.

I little confused even if I agree with you. Looks like we are talking about template function will degrade performance compared with pure C implementation of qsort(), but you are talking template function could improve performance by inline some code like compare for POD types?

regards,
George

A template function will never degrade performance just because it's a template function. It can increase the size of the executable, but that's all.

After discussing with you and have some self-study, the original point of this question still makes me confused -- but not as much as before, there are mainly two points about throwing exception from C++ into C caller code, and such code is not uncommon since a part of STL uses C implementation CRT.

1. Undefined behavior since C know nothing about C++ exception;

2. Defined behavior, and object on C stack will be released but other resources are not. C++ exception is pased through C code into higher memory address to look for exception handler.

Which option do you agree?

Originally Posted by Elysia

Correction: can be inlined. It's up to the compiler in the end whether or not it inlines it.

But I think there are lots of code nowadays invoking C function like qsort and provide throwable C++ callback code, agree? :-)

regards,
George

I doubt that more than 0.01% of the code calling qsort uses code that MAY throw - why would you ever throw in a function that essentially does the same as "operator>"? It is conceivable to come up with a function that DOES throw an exception, but it would be bad practice, and what are the circumstances where you expect this to be the case?

Beyond and comparison operator, do you think there are any other possibilities to throw in callback provided to qsort()?

Originally Posted by matsp

I doubt that more than 0.01% of the code calling qsort uses code that MAY throw - why would you ever throw in a function that essentially does the same as "operator>"? It is conceivable to come up with a function that DOES throw an exception, but it would be bad practice, and what are the circumstances where you expect this to be the case?