Inline Functions
Inline function क्या है?
सामान्य function call की एक छोटी कीमत होती है: program function पर jump करता है, arguments भेजता है और वापस आता है। "दो numbers में बड़ा लौटाओ" जैसे एक line के function में यह overhead असली काम से भी ज़्यादा हो सकता है। Inline function compiler से कहता है कि function का body सीधे हर call वाली जगह पर रख दे, ताकि jump ही न हो।
inline एक अनुरोध है, आदेश नहीं। Inline करना फ़ायदेमंद है या नहीं, यह compiler तय करता है।यह lesson beginners के लिए है — syntax, उदाहरण, फ़ायदे और सीमाएँ। Headers और One Definition Rule के गहरे नियमों के लिए देखें C++ inline functions: ODR, headers और performance।
Syntax
inline return_type function_name(parameters) {
// छोटा body
}Keyword function की definition में return type से पहले लिखा जाता है।
उदाहरण: square और maximum
#include <iostream>
inline int square(int x) {
return x * x;
}
inline int maxOf(int a, int b) {
return (a > b) ? a : b;
}
int main() {
std::cout << "Square of 7 : " << square(7) << "\n";
std::cout << "Max(45, 62) : " << maxOf(45, 62) << "\n";
int marks[] = {78, 91, 66, 84};
int best = marks[0];
for (int m : marks) best = maxOf(best, m);
std::cout << "Highest mark: " << best << "\n";
return 0;
}अगर compiler square(7) को inline करे, तो बना code ऐसा होगा जैसे आपने सीधे 7 * 7 लिखा हो — कोई call नहीं।
Inline member functions
Class body के अंदर define किया गया member function अपने-आप inline होता है:
class Circle {
double r;
public:
Circle(double radius) : r(radius) {}
double area() const { return 3.14159 * r * r; } // अपने-आप inline
double perimeter() const; // सिर्फ declared
};
inline double Circle::perimeter() const { // बाहर defined: inline जोड़ें
return 2 * 3.14159 * r;
}Getters और setters ऐसे छोटे functions का classic उदाहरण हैं जिन्हें इसका फ़ायदा मिलता है।
Inline function vs macro
C में programmers speed के लिए macros लेते थे। Macro वाले तरीके में छिपे जाल हैं:
#define SQUARE_M(x) ((x) * (x))
inline int squareF(int x) { return x * x; }
int i = 3;
int a = SQUARE_M(i++); // i दो बार बढ़ता है — गलत/undefined
int j = 3;
int b = squareF(j++); // j एक बार बढ़ता है, b = 9| बिंदु | Macro | Inline function |
|---|---|---|
| कौन process करता है | Preprocessor (text) | Compiler (असली function) |
| Type checking | नहीं | हाँ |
| Arguments evaluate | शायद कई बार | ठीक एक बार |
| Scope और namespaces | अनदेखा | माना जाता है |
| Debugging | मुश्किल | सामान्य |
Compiler कब inline नहीं करेगा?
Compiler आमतौर पर मना करता है (या फ़ायदा नहीं होता) जब function:
- बड़ा हो या उसमें loops,
switchयाgotoहों, - recursive हो,
- उसमें
staticvariables हों, - function pointer से call हो या उसका address लिया गया हो,
virtualहो और base pointer से call हो (run time पर तय)।
Optimisation (-O2) वाले modern compilers बिना inline लिखे छोटे functions को भी inline कर देते हैं। इसलिए inline headers में define होने वाले छोटे functions के लिए लें, बड़े functions की speed trick की तरह नहीं।
फ़ायदे और नुकसान
| फ़ायदे | नुकसान |
|---|---|
| छोटे functions में call/return overhead नहीं | Code size बढ़ता है — body हर call पर copy होता है |
| Call वाली जगह पर compiler और optimisation कर पाता है | बड़ा code CPU cache पर असर डालकर धीमा भी कर सकता है |
| Macros से सुरक्षित (type-checked) | Header में inline function बदलने पर उसे include करने वाली हर file फिर compile होती है |
| कई files में इस्तेमाल होने वाले header में define हो सकता है | बड़े या recursive functions के लिए ठीक नहीं |
आम गलतियाँ
- बड़े या recursive functions को
inlineलिखकर speed की उम्मीद करना। - Header में function को
inlinedeclare करना पर definition.cppमें रखना — दूसरी files में "undefined reference" errors। Inline functions वहीं define होने चाहिए जहाँ इस्तेमाल हों, आमतौर पर header में। - यह मानना कि
inlineसे inlining पक्की है। - जहाँ inline (या
constexpr) function सुरक्षित हो, वहाँ macro लेना।
Inline functions cube(int), isEven(int) और celsiusToFahrenheit(double) लिखें। इन्हें values के array पर loop में इस्तेमाल करें, और यही cube macro के रूप में लिखकर देखें कि cube(i++) के साथ क्या होता है।
सारांश
inlinecompiler से calls की जगह function body रखने को कहता है।- बहुत छोटे, बार-बार call होने वाले functions के लिए सबसे अच्छा।
- Class के अंदर define किए गए member functions अपने-आप inline होते हैं।
- Inline functions type-safe हैं और arguments एक बार evaluate करते हैं — macros के विपरीत।
- Compiler
inlineको अनदेखा कर सकता है; बड़े या recursive functions पर न लगाएँ।
संबंधित: functions, preprocessor directives, inline functions विस्तार से।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में inline function क्या है?
inline keyword लगा हो (या जो class body के अंदर define हो)। यह compiler से कहता है कि हर call की जगह function का code ही रख दे, सामान्य function call करने के बजाय — इससे बहुत छोटे functions में call का overhead बच सकता है।क्या compiler हमेशा inline function को inline करता है?
inline सिर्फ एक अनुरोध है। बड़े, recursive, loops या static variables वाले, या जिनका address लिया गया हो, ऐसे functions के लिए compiler इसे अनदेखा कर सकता है — और बिना inline लिखे छोटे functions को भी inline कर सकता है। आज यह keyword मुख्य रूप से function को header में define करने की अनुमति देता है।Inline function और macro में क्या अंतर है?
क्या class के अंदर define किए गए member functions inline होते हैं?
inline keyword लिखने की ज़रूरत नहीं।