Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
GCC attributes
thomas:
--- Quote from: killerbot on December 18, 2005, 05:12:15 pm ---The keyword inline is not that interesting, since it is just a hint to the compiler !!!
Secondly, methods being implemented within the class definition/body are , according to the standard, to be inlined !!!
--- End quote ---
So what do you want to tell me? Functions defined inside the class scope are only inlined in optimized build.
Code::Blocks is currently being built and distributed non-optimized. In the special case of the code completion parser, this makes a difference of nearly 100% in execution time. This was one reason for using function attributes (in particular always_inline).
The inline keyword gives an additional hint to the compiler that I wish these functions to be inlined, optimized build or not. Maybe the compiler will ignore the hint, maybe not. There is not much harm in using it. However, if that makes you feel that I don't know C++, then I'll remove it.
killerbot:
Thomas,
Methods implemented in the class body are ALWAYS inlined (I think this is guranteed by the standard) !!!
class MyClass
{
void MyFunction1() const {return something;} // <<---- always inlined
inline void MyFunction2(); //<<-- implemented somewhere else (90% chance this inline suggestion will be ignored)
}
I don't doubt your C++ knowledge !! That sounded very negative, I hope it did not give you this impression.
thomas:
--- Quote from: killerbot on December 18, 2005, 09:59:20 pm ---Methods implemented in the class body are ALWAYS inlined (I think this is guranteed by the standard) !!!
--- End quote ---
--- Quote from: gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Optimize-Options.html#Optimize-Options ---[...] Otherwise, when you specify -O, member functions defined inside class scope are compiled inline by default
--- End quote ---
me22:
--- Quote from: killerbot on December 18, 2005, 09:59:20 pm ---Methods implemented in the class body are ALWAYS inlined (I think this is guranteed by the standard) !!!
class MyClass
{
void MyFunction1() const {return something;} // <<---- always inlined
inline void MyFunction2(); //<<-- implemented somewhere else (90% chance this inline suggestion will be ignored)
}
--- End quote ---
Correction: functions defined inside a class definition are inline-qualified.
Note, however, that that does NOT mean "always inlined." inline is a request, not an order. Compilers are permitted to completely ignore the request, and many do.
GOTW #33: Inline covers this nicely:
--- Quote ---Inline Difficulty: 4 / 10
Contrary to popular opinion, the keyword inline is not some sort of magic bullet. It is, however, a useful tool when employed properly. The question is, When should you use it?
--- End quote ---
It's also mentioned in GOTW #27:
--- Quote ---This one is a matter of judgment. In short, prefer to write all functions out-of-line by default, and then selectively inline individual functions as necessary only after you know that the performance gain from inlining is actually needed.
If you inline the function, the positive side is that you avoid the overhead of the extra function call to f.
The negative side is that inlining f exposes f's implementation and make client code depend on it, so that if f changes all client code must recompile. Worse, client code now also needs at least the prototype for function g(), which is a bit of a shame since client code never actually calls g directly and probably never needed g's prototype before (at least, not as far as we can tell from our example). And if g() itself were changed to take other parameters of still other types, client code would now depend on those classes' declarations, too.
Both inlining or not inlining can be valid choices. It's a judgment call where the benefits and drawbacks depend on what you know about how (and how widely) f is used today, and how (and how often) it's likely to change in the future.
From the GotW coding standards:
- prefer passing parameters of class type by const& rather than passing them by value
- avoid inlining functions until profiling tells you it's justified (programmers are notoriously bad at guessing which parts of their code are performance bottlenecks)
--- End quote ---
( Also, void functions can't return something :lol: )
killerbot:
Yes, me22 I have read this also in the bookd of Herb Sutter, and those of Scott Meyers.
Last month I was in a workshop of Scott Meyers about the preformance of STL, and using it the smart way. There was a topic about sorting and using functors, and if I recall correctly he mentioned that functors are better then regular functions, since many functors are small classes or structs, where the operator() is implemented directly in the class/struct defintion and therefor is inlined (I think he said the standard guarantees that). I will browse through the printed slides to see if I can find it black on white.
If my statement from above is correct, then maybe we might have a compiler behaving differently. I'll look for some more documentation on the subject.
kind regards,
Lieven
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version