![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 84
Rep Power: 4
![]() |
An Inline Question
Hi
I have a little problem regarding inline functions ,does declaring a function inside the class definition make it inline or not.
__________________
"You're good... but me, I'm magic" |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5
![]() |
Short answer is "Maybe".
If a function is declared within the class definition, but no definition (i.e. body) is supplied with that declaration, then the function is not typically inline .... although some aggressive compilers (or linkers) might inline the function anyway. If the function is defined within the class definition (i.e. a body of the member function is given within the class declaration) that is normally a hint to the compiler to inline the function. However, the compiler is allowed to ignore that hint ... some compilers do, some don't. If the function is explicitly declared inline (i.e. the function definition comes after the class declaration, and is prefixed by the inline keyword) then, again, the function might be inlined. Like any form of inlining, use of the inline keyword is a hint to the compiler and compilers are allowed to ignore that hint. |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Oct 2005
Posts: 84
Rep Power: 4
![]() |
Quote:
so to be on safer side i should declare n define the small function inside the class
__________________
"You're good... but me, I'm magic" |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5
![]() |
I suggested nothing of the sort. Inlining is not inherently a good (or safe) thing.
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Oct 2005
Posts: 84
Rep Power: 4
![]() |
Quote:
if it makes your application faster why is it not good
__________________
"You're good... but me, I'm magic" |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Suppose you have a class in some file that has some defined interface, and that some method will be inlined in a second file. If the class author changes the class in some way that doesn't change its functionality or interface (perhaps just reorders or adds some variables), the inlined code in your second file is likely to break.
In other words, encapsulation failed. You now have to know when this useful library has changed, so that you can recompile all your code that uses it. If the method were not in-lined, everything would still work.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5
![]() |
Quote:
for(int x=0; x<100; ++x)
{
if(x % 10)
someFunction(x);
else
// some other code here
}These considerations depend heavily on the architecture involved, coupled with what other optimizations may be enabled (loop unrolling, for example, will also have an impact on whether or not this code is cache-friendly). This is precisely why the compiler only takes the 'inline' keyword as a hint; it will do whatever it determines as best. A similar issue exists with the use of the register keyword.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5
![]() |
Additionally, inlining is also one of the tools of the programmer who I refer to as the "premature optimiser" who, in too many case, becomes the "excessive optimiser". Such a person believes that faster performance is inherently a good thing, and therefore spends a lot of time optimising the performance of their code. Behaviours of the premature optimiser include buffing and polishing of loops and attempting to inline everything as much as possible. In the premature optimiser also often focuses on performance so much that they compromise program correctness (the program runs fast, but wrong) or, even more commonly, take days crafting their code when colleagues may take hours. The effect is programs that take longer to develop, and more likely to have obscure bugs .... which often do not show up until the program is in the hands of users, because the test cases used by the premature optimiser are incomplete.
It is generally better to get the program correct (in terms of producing the results it needs to produce) before worrying about its speed. If a program produces the results it needs to produce, but is not fast enough, then tools like profilers can be used to find hotspots in code for optimisation of performance. Interestingly enough, in most non-trivial systems, the actual hotspots turn out to be something completely different from the places where a premature optimiser focuses attention. The main exception to my position (focus on correctness first, rather than performance) arises in high-criticality code (eg safety critical) where timing performance can be a key to correct functioning -- there can be hard realtime constraints (both upper and lower bounds on execution times for some functions). In such systems, you will not get away with coding and performance tweaking of code ..... the expectation, in relevant standards, is that considerable design and analysis effort is MANDATORY before any LINE of code is cut. Essentially, in high criticality systems, the analysis effort needs to consider performance. And, if such analysis is applied to the process of developing a system in C/C++, it is possible but difficult to justify inlining of any functions. Code constructs that tap into discretionary behaviour of a compiler (i.e. even if asked to do inlining, the compiler may decide not to) is quite difficult to analyse. Premature optimisers, typically, do not find employment in development of high criticality systems. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
No, they get jobs in they tend to get jobs in the game industry
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Inline function with local statics. | Eoin | C++ | 2 | Jan 23rd, 2007 6:01 PM |
| Attitudes | Oddball | Coder's Corner Lounge | 29 | Mar 18th, 2006 9:34 PM |
| How to post a question | nnxion | C++ | 10 | Jun 3rd, 2005 11:53 AM |
| How to post a question | nnxion | C++ | 0 | Jun 3rd, 2005 8:55 AM |
| How to post a question | nnxion | C | 0 | Jun 3rd, 2005 8:55 AM |