SYSTEM & SOFTWARE LAB
¿¬±¸½Ç¼Ò°³ Áöµµ±³¼ö ³í¹®/ƯÇã/º¸°í¼­ ÇÁ·ÎÁ§Æ® ¼¼¹Ì³ª °­ÀÇ½Ç °Ô½ÃÆÇ ÀÚ·á½Ç °ü·Ã»çÀÌÆ®
 

°ø°³°­ÁÂ
Áö½Ä±â¹Ý½Ã½ºÅÛ
°øÇб³À°»ó´ãÁöµµ
¼ÒÇÁÆ®¿þ¾î°øÇÐ
ÀÓº£µðµåOS
ÄÄÇ»ÅÍÇÁ·Î±×·¡¹Ö(4,6,8ºÐ¹Ý)
ÄÄÇ»ÅÍÇÁ·Î±×·¡¹Ö(3,5ºÐ¹Ý)
USNÀÀ¿ë¼ÒÇÁÆ®¿þ¾î¼³°è
ÀÓº£µðµå½Ã½ºÅÛÇÁ·Î±×·¡¹Ö
ÀÓº£µðµå½Ã½ºÅÛ¼ÒÇÁÆ®¿þ¾î
ÀڷᱸÁ¶
ÄÄÇ»ÅÍÇÁ·Î±×·¡¹Ö(9,10ºÐ¹Ý)




  Inline functions 2006-10-09 21:53:01  
  À̸§ : ÇÑ¿ì¶÷  (210.115.¢½.¢½)  Á¶È¸ : 2074    

[9] Inline functions
(Part of C++ FAQ Lite, Copyright © 1991-96, Marshall Cline, cline@parashift.com)





FAQs in section [9]:








[9.1] What's the deal with inline functions?


An inline function is a function whose code gets inserted into the caller's code stream. Like a #define macro, inline functions improve performance by avoiding the overhead of the call itself and (especially!) by the compiler being able to optimize through the call ("procedural integration").

[ Top | Bottom | Previous section | Next section ]





[9.2] How can inline functions help with the tradeoff of safety vs. speed?


In straight C, you can achieve "encapsulated structs" by putting a void* in a struct, in which case the void* points to the real data that is unknown to users of the struct. Therefore users of the struct don't know how to interpret the stuff pointed to by the void*, but the access functions cast the void* to the approprate hidden type. This gives a form of encapsulation.

Unfortunately it forfeits type safety, and also imposes a function call to access even trivial fields of the struct (if you allowed direct access to the struct's fields, anyone and everyone would be able to get direct access since they would of necessity know how to interpret the stuff pointed to by the void*; this would make it difficult to change the underlying data structure).

Function call overhead is small, but can add up. C++ classes allow function calls to be expanded inline. This lets you have the safety of encapsulation along with the speed of direct access. Furthermore the parameter types of these inline functions are checked by the compiler, an improvement over C's #define macros.

[ Top | Bottom | Previous section | Next section ]





[9.3] Why should I use inline functions? Why not just use plain old #define macros?


Because #define macros are evil.

Unlike #define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:

// A macro that returns the absolute value of i
#define unsafe(i) \
( (i) >= 0 ? (i) : -(i) )

// An inline function that returns the absolute value of i
inline
int safe(int i)
{
return i >= 0 ? i : -i;
}

int f();

void userCode(int x)
{
int ans;

ans = unsafe(x++);
// Error! x is incremented twice
ans = unsafe(f());
// Danger! f() is called twice

ans = safe(x++);
// Correct! x is incremented once
ans = safe(f());
// Correct! f() is called once
}

Also unlike macros, argument types are checked, and necessary conversions are performed correctly.

Macros are bad for your health; don't use them unless you have to.

[ Top | Bottom | Previous section | Next section ]





[9.4] How do you tell the compiler to make a non-member function inline?


When you declare an inline function, it looks just like a normal function:

void f(int i, char c);

But when you define an inline function, you prepend the function's definition with the keyword inline, and you put the definition into a header file:

inline
void f(int i, char c)
{
// ...
}

Note: It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.

[ Top | Bottom | Previous section | Next section ]





[9.5] How do you tell the compiler to make a member function inline?


When you declare an inline member function, it looks just like a normal member function:

class Fred {
public:
void f(int i, char c);
};

But when you define an inline member function, you prepend the member function's definition with the keyword inline, and you put the definition into a header file:

inline
void Fred::f(int i, char c)
{
// ...
}

It's usually imperative that the function's definition (the part between the {...}) be placed in a header file. If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker.

[ Top | Bottom | Previous section | Next section ]





[9.6] Is there another way to tell the compiler to make a member function inline?


Yep: define the member function in the class body itself:

class Fred {
public:
void f(int i, char c)
{
// ...
}
};

Although this is easier on the person who writes the class, it's harder on all the readers since it mixes "what" a class does with "how" it does them. Because of this mixture, we normally prefer to define member functions outside the class body with the inline keyword. The insight that makes sense of this: in a reuse-oriented world, there will usually be many people who use your class, but there is only one person who builds it (yourself); therefore you should do things that favor the many rather than the few.

[ Top | Bottom | Previous section | Next section ]





[9.7] Are inline functions guaranteed to make your performance better?


Nope.

Beware that overuse of inline functions can cause code bloat, which can in turn have a negative performance impact in paging environments.

[ Top | Bottom | Previous section | Next section ]



E-mail the author
[ C++ FAQ Lite | Table of contents | Subject index | About the author | © | Download your own copy ]
Revised Jan 1, 1997



########################################################################################
±èµ¿±¹ ±Û¿¡ ³Ê¹« ¸¹Àº ±¤°í¼º ´ñ±ÛÀÌ ´Þ·Á »èÁ¦ ÈÄ Àç µî·Ï
########################################################################################


  John Williams 08-08-21 05:29  
Pretty nice site, wants to see much more on it! :)
92.48.¢½.¢½
  LeonEbaaqzzrep 08-08-21 23:59  
about chris browns music styz music group party time dj music mike abernathy  <a href= http://musicresource.org/artist2422/muslimgauze/ >Muslimgauze - Download all albums from MP3 Archive</a>   ethnomusicology articles on taarab music where can i get free music for comercial use music educator degrees , this is my country  piano sheet music twighlight zone music baroque music 365 free  <a href=http://newmusics.org/artist20434/jack-frost/>Jack Frost</a>   american guild of music chris oleksy classical chinese music for the longest time sheet music , hallelujah sheet music bill gaithers music lyrics halloween chase scene music jaime lee curtis  <a href=http://mymusicpro.org/artist25224ip/irakere-audio/>Listen free Irakere Music Online</a>   what are the most popular downloads and latest music jim cole christian music how do you keep the music playing chris botti , music to lottery by chris brown brian jackson music software change keys music  <a href=http://royalmp3.net/artist34175/supermayer-discography/>Download albums of singer Supermayer - MP3 Music Free Download</a>   fashion music 1997 calendar pirelli top 10 latest music single releases slow down music pich software practice , good music downloads music jukebox software with free song tagging nc music therapy  <a href=http://newmusics.org/artist30390/soulhead/>Soulhead song lyrics</a>   music match free version codelyoko music only naturalizer shoe music sale , tropical music define wisdom music smokey mountain music  <a href=http://musicresource.org/artist10700/she-raw/>She-Raw</a>   michelle thomas in the music industry snare sheet music schmitt music pensacola 
oceano music sheet guitar and piano tabs music music in the classroom  <a href= http://internetmp3.org/artist6828/the-waterboys-discography/ >Download albums of singer The Waterboys - MP3 Music Free Download</a>   third grade program music online   instrumental music   radio nursery rhyme music , yaov music aaron cameron music walgreens in store music  <a href=http://mymusicpro.org/artist13568ip/daylight-dies-audio/>Daylight Dies</a>   music down loads for idiots west side story   maria   sheet music   eb major latin music translation , music software mixer what are the differences between shona andchimurenga music free viola sheet music harry potter  <a href= http://newmusics.org/artist31726/cagedbaby/ >Cagedbaby</a>   led zeppelin music downloads where can i listen to bagpipe music online great prices on music downloads .
92.48.¢½.¢½

 
ÀÚµ¿µî·Ï¹æÁö : 267a89353f   ¿ÞÂÊÀÇ ±ÛÀÚÁß »¡°£±ÛÀÚ¸¸ ¼ø¼­´ë·Î ÀÔ·ÂÇϼ¼¿ä.

À̸ðƼÄÜ »ç¿ë 
¸ñ·Ï ±Û¾²±â
°Ô½Ã¹° 105°Ç  
No Title Name Date Hits
115   Linux Network Install (using NFS/FTP/HTTP) byfun ¹ÚÃѸí 06-03-06 3228
114   [HW] RS232 Æ÷Æ® Á¦¾î ¿¹Á¦ (3) winhoho ±èµ¿Çõ 05-03-27 12602
113   [MFC] ´ÙÀ̾ó·Î±×(Dialog)À§¿¡ Åø¹Ù(ToolBar) ºÙÀ̱â (4) jinnie4u ÀÌÁÂÇü 06-06-25 6524
112   Á¦°¡ ¾´ ±ÛÀ» ¿­¾îº¼ ¼ö°¡ ¾ø³×¿ä... ÀÌÈﺹ 07-12-09 1390
105   [Æß] XML - SAX °ú DOM API »ç¿ë fastpopo ¹æÇѹΠ07-03-08 1494
104   [Æß] JAVA RMI À» ÀÌ¿ëÇÏ¿© À̱âÁ¾ÀÇ DB¿¡ Á¢±ÙÇϱâ fastpopo ¹æÇѹΠ07-03-08 1260
103   java ¿¡¼­ byte ¹è¿­ 4°³¸¦ ¸ð¾Æ¼­ int ·Î º¯È¯ ÇÒ¶§!! fastpopo ¹æÇѹΠ07-01-30 1687
102   Visual Studio Project Renamer jinnie4u ÀÌÁÂÇü 06-10-23 3103
  Inline functions (2) ÇÑ¿ì¶÷ 06-10-09 2075
100   ½ºÇÉ ÄÁÆ®·Ñ ÀÀ¿ë(up, down) | MFC jinnie4u ÀÌÁÂÇü 06-07-19 2295
95   [ÀÓ½Ã] µðÁöÅнýºÅÛ ½ÇÇè 4ÁÖÂ÷ 5ÁÖÂ÷ ½Ç½ÀÀÚ·á dkkim ±èµ¿±¹ 06-04-03 1557
91   [Æß]named pipe(FIFOs) jinnie4u ÀÌÁÂÇü 05-12-01 3121
89   [Æß] latex¿¡¼­ multicolumn°ú multirow»ç¿ë¿¹ jinnie4u ÀÌÁÂÇü 05-11-09 2158
88   ¸®´ª½º ¼­¹ö ±âº» º¸¾È Á¤Ã¥ byfun ¹ÚÃѸí 05-09-13 1541
87   [MFC] ÇÁ·Î±×·¥À» Çϳª¸¸ ½ÇÇàÇϱâ byfun ¹ÚÃѸí 05-09-02 4963

±Û¾²±â
[óÀ½][ÀÌÀü][1] 2 [3][4][5]...[´ÙÀ½][¸Ç³¡]
 
Kangwon Univ. Dept. of Computer Imformation and Communications Engineering Software and System Lab.