Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 67

Inline Functions

C++ में inline का सही अर्थ

inline compiler को call-site substitution prefer करने का nonbinding संकेत देता है; compiler मना कर सकता है। Modern C++ में महत्वपूर्ण role ODR है: equivalent definitions multiple translation units में होकर भी one entity रह सकती हैं।

inline int square(int value) noexcept {
 return value * value;
}
Exam answer: inline हर function call हटाने का command नहीं; यह language/ODR facility और optimization preference है।

Syntax, Program और Output

#include <iostream>
inline int square(int value) noexcept { return value*value; }
inline int max_of(int a,int b) noexcept { return a>b?a:b; }
int main(){
 std::cout << square(7) << '\n';
 std::cout << max_of(18,25) << '\n';
}
49 25

Output semantics prove करता है, substitution नहीं। Debug build calls रख सकता है; optimized build constant-fold भी कर सकता है।

Headers और One Definition Rule

// math_utils.hpp
#ifndef MATH_UTILS_HPP
#define MATH_UTILS_HPP
inline int cube(int n) noexcept { return n*n*n; }
#endif

Header कई .cpp में include हो सकती है। सभी definitions ODR-equivalent हों और name lookup consistent हो। Macro conditions से अलग bodies बनाना गलत है।

Include guard one translation unit में repeat include रोकता है; inline multi-translation-unit definition rule देता है। दोनों अलग problems solve करते हैं।

Implicit Inline Cases

class Counter {
public: int value() const noexcept { return value_; }
private: int value_{};
};
constexpr int twice(int n) noexcept { return n*2; }
inline constexpr int max_students=40;
  • Class definition के अंदर function normally implicitly inline।
  • First declaration पर constexpr/consteval implicitly inline।
  • Templates normally headers में defined।
  • C++17 inline variables one header entity support करते हैं।

फिर भी machine-code substitution guarantee नहीं।

Performance Reality

Substitution call overhead हटाकर optimization expose कर सकती है; duplication binary size/cache pressure बढ़ा सकती है। Small hot accessor candidate है; large/rare/branch-heavy function poor candidate।

g++ -std=c++20 -O2 -DNDEBUG -Wall -Wextra app.cpp
g++ -std=c++20 -O2 -S app.cpp

Representative release build/profile से measure करें। Microbenchmark dead-code elimination, warm-up और repeated samples handle करे।

Classes, Templates और Static Locals

template<typename T>
inline const T& clamp_low(const T& value,const T& low){
 return value<low?low:value;
}
inline int next_request_id(){
 static int id=0; return ++id;
}

Inline function का local static translation units में one shared entity है। Plain increment thread-safe नहीं; race में atomic/lock चाहिए। Templates को header में होने मात्र से explicit inline जरूरी नहीं। fundamentals और headers पढ़ें।

Common Mistakes

MistakeCorrection
Guaranteed speedProfile करें
Macro से different definitionsOne equivalent body
Large public-header bodyOut-of-line consider करें
Small function macroTyped constexpr function
Recursion fully expandsReal calls expect करें
#define SQUARE(x) ((x)*(x))
constexpr int square_safe(int x) noexcept { return x*x; }

Macro argument twice evaluate कर सकता है; function type/scope/one evaluation देता है।

Review और Practice

  1. Substitution not guaranteed?
  2. Definition reachable?
  3. ODR-equivalent definitions?
  4. Include guard?
  5. Header size/compile impact?
  6. constexpr better?
  7. Multiple files में warnings/tests?
  8. Performance measured?

Practice: cube header दो cpp files में include/link करें। Inline हटाकर error observe करें; फिर definition एक cpp में move करके correct alternative compare करें।

Authoritative संदर्भ

Rules और cautions working draft/Core Guidelines से verified हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या inline keyword compiler को call inline करने पर मजबूर करता है?
नहीं। Implementation body substitute कर सकती है या normal call रख सकती है। Optimization decision, inline के linkage/ODR rules से अलग है।
Inline functions headers में क्यों define होते हैं?
Definition required जगह reachable होनी चाहिए। External-linkage inline entity की equivalent definitions multiple translation units में हो सकती हैं और ODR के अनुसार one entity रहती है।
Class के अंदर defined function automatically inline है?
Usual non-module case में class definition के अंदर function implicitly inline है। इससे call substitution guarantee नहीं होती और large member को in-class रखना जरूरी नहीं।
क्या हर constexpr function inline है?
First declaration पर constexpr/consteval function implicitly inline है। Constant evaluation required न हो तो runtime पर भी चल सकता है।
क्या inline program slow कर सकता है?
Excessive substitution code size और instruction-cache pressure बढ़ा सकती है। Optimized build measure करें; intuition पर inline न जोड़ें।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicFriend Function & Class आपकी प्रगति: 67 / 81 (83%) · Lesson 68 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें