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;
}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';
}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; }
#endifHeader कई .cpp में include हो सकती है। सभी definitions ODR-equivalent हों और name lookup consistent हो। Macro conditions से अलग bodies बनाना गलत है।
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.cppRepresentative 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
| Mistake | Correction |
|---|---|
| Guaranteed speed | Profile करें |
| Macro से different definitions | One equivalent body |
| Large public-header body | Out-of-line consider करें |
| Small function macro | Typed constexpr function |
| Recursion fully expands | Real 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
- Substitution not guaranteed?
- Definition reachable?
- ODR-equivalent definitions?
- Include guard?
- Header size/compile impact?
- constexpr better?
- Multiple files में warnings/tests?
- 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 हैं।
