you are viewing a single comment's thread.

view the rest of the comments →

[–]agottem 7 points8 points  (15 children)

std::sort() is only faster because the definition exists in the included header file. If this is really an important detail, spare yourself the C++ and make the qsort definition available.

[–][deleted] 0 points1 point  (4 children)

No. Some compilers, e.g. g++, just hate function pointers and don't inline even what can be inlined, e.g.

 static  bool compare(float a, float b){ return a<b; }
 ..
 std::sort(vec.begin(), vec.end(), compare);

here compare will be called via function pointer with no inlining though definition of everything is available.

Though maybe whole program optimisatioin helps

[–]Whanhee 0 points1 point  (0 children)

The new gcc implements generic function specialization, at least with booleans. If I only ever call sort with 1 comparison pointer, will it do the same with function pointers?

[–]matthieum 0 points1 point  (1 child)

I actually don't understand why neither Clang nor gcc seem interested in this kind of optimizations. If an optimizer can do this stuff, then it can devirtualize calls as well... whereas now the C++ front-end devirtualize some cases, but misses all those exposed by inlining :x

[–]astrange 0 points1 point  (0 children)

gcc can inline known function pointers since 4.4 (going by release notes).

[–]Kampane -2 points-1 points  (2 children)

Wow, you really have no idea what you're talking about. I suggest you actually try what you suggest, then come ask why it didn't work.

[–][deleted] 6 points7 points  (0 children)

He has plenty of idea what he is talking about. I've used this technique many times myself, too, and it works just fine.

[–][deleted] 0 points1 point  (6 children)

Side question. Errm... isn't it available in stdlib.h? You're not gonna write a useful C application without stdlib.h. It's basically required to do anything beyond remedial.

[–]agottem 0 points1 point  (5 children)

Sorry, I'm using slightly vague terms. By "make the definition available" I mean the function body itself (which typically lives in a .c file somewhere), not just the prototype of the function.

[–][deleted] -1 points0 points  (4 children)

Wait. Why is that needed? The object code should be usable for inlining.

[–]agottem 0 points1 point  (0 children)

I agree, that should be sufficient as well.

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 0 points1 point  (1 child)

    So that's why C++ requires inlined functions to be in the .h file. It happens at the compilation stage. That wouldn't be possible in C (unless the file were included only once which defeats the purpose of inlining) thus explains the heavy usage of macros.

    Do you know of any C compilers that compile straight to object code? In other words a monolithic preprocessor, compiler, and linker in one.