विषय पर बढ़ें

अंतिम अद्यतन:2026-10-12

C++ const परिवार

रूपरेखा

  • const
  • constexpr
  • C++20
  • consteval
  • constinit

नोट: इस लेख के सभी कोड उदाहरण Knowledge Planet पर दिए गए हैं।

1.const

const C++/C सीखते समय सबसे पहले मिलने वाले कीवर्ड्स में से एक है, आमतौर पर cv-qualifier की तरह उपयोग होता है, जो दर्शाता है कि ऑब्जेक्ट immutable है। यह compile-time evaluation की गारंटी नहीं देता, कब evaluation होगा यह कंपाइलर तय करता है। उदाहरण के लिए:

const int a = 1; // 编译时
const auto x = getdata(); // 运行时

const कभी-कभी compile-time constant expression में उपयोग हो सकता है, लेकिन कुछ अपवाद हैं, जैसे:

const double count_1 = 3.3;
std::array<double, static_cast<int>(count_1)> moreDoubles1 {1.1, 2.2, 3.3};

इस स्थिति में error आएगा: the value of 'count_1' is not usable in a constant expression

लेकिन ऐसा करना सही है:

const int count = 3;
std::array<double, count> doubles {1.1, 2.2, 3.3};

इसका मूल कारण compile-time expression evaluation है, इसलिए C++11 ने constexpr लाया।

2.constexpr

constexpr से निर्दिष्ट किया जा सकता है कि वेरिएबल की वैल्यू constant expression में आ सकती है, इसके लिए बस पिछले उदाहरण का const बदलकर constexpr करना है, फिर यह सही चलेगा।

constexpr double count_2 = 3.3;
std::array<double, static_cast<int>(count_2)> moreDoubles2 {1.1, 2.2, 3.3}; 

constexpr का मतलब यह नहीं कि यह केवल compile time पर ही चलेगा। इसका असली अर्थ है कि यह compile time पर calculate हो सकता है, पर अगर runtime में constexpr वेरिएबल या फ़ंक्शन का उपयोग किया जाए, तो यह runtime पर भी निष्पादित हो सकता है।

उदाहरण के लिए: result1 और result2 बिल्कुल अलग हैं, result1 compile time पर calculate होता है, जबकि result2 runtime पर, एक सवाल: क्या आप जानते हैं क्यों?

इसलिए constexpr लिखा फ़ंक्शन ज़रूरी नहीं कि हमेशा compile time पर ही calculate हो।

constexpr int compileTimeSum(int a, int b) {
    return a + b;
}

int main() {
    constexpr int result1 = compileTimeSum(3, 5); 
    int x = 10;
    int result2 = compileTimeSum(x, 20);  
    return 0;
}

तो इसकी गारंटी कौन देता है? तब C++20 आया।

3.C++20

3.1 consteval

consteval ऊपर की समस्या हल करता है, यह केवल फ़ंक्शन पर उपयोग होता है और सभी कॉल को compile time पर होने पर मजबूर करता है। अगर हम ऊपर का constexpr बदलकर consteval कर दें, तो result2 का क्या होगा?

तो error आएगा: call to consteval function 'compileTimeSum(x, 20)' is not a constant expression

consteval int compileTimeSum(int a, int b) {
    return a + b;
}

int main() {
    constexpr int result1 = compileTimeSum(3, 5); 
    int x = 10;
    int result2 = compileTimeSum(x, 20);  
    return 0;
}

अगर consteval को वेरिएबल पर उपयोग करें, तो भी error आएगा: error: a variable cannot be declared 'consteval'।

https://en.cppreference.com/w/cpp/language/consteval

3.2 constinit

constinit static या thread local वेरिएबल को constant initialization पर मजबूर कर सकता है।

  • constexpr, consteval के साथ उपयोग नहीं हो सकता
  • यह एक non-type specifier है, इसका काम assert जैसा है
  • गारंटी देता है कि वेरिएबल static initialize होगा
  • केवल static या thread_local पर उपयोग होता है

उदाहरण के लिए: नीचे का compile error देगा: error: 'constinit' can only be applied to a variable with static or thread storage duration

constinit int a = 100;  
int main() {  
      ++a; 
      constinit auto b = 100;  // error
      // b has thread storage duration
      constinit thread_local auto b = 100;
}

संबंधित विषय

टिप्पणियाँ