← Back to C++ Course | Chapter 16: Modern C++ | Lesson 6 of 9

C++ का constexpr

constexpr C++ को बताता है कि एक value या function program चलने से पहले ही work out किया जा सकता है, पहले से homework करने की तरह। यह programs को तेज़ और values fixed बनाता है।
Syntax
cpp
constexpr data_type variable_name = compile_time_value;

constexpr return_type function_name(parameters) {
  return expression;
}

constexpr क्या है?

constexpr एक expression या function को compile-time-constant inputs दिए जाने पर compile time पर evaluable की तरह mark करता है, compiler को result compilation के दौरान एक बार compute करने देते हुए, program हर बार चलने पर इसे recalculate करने की बजाय।

उदाहरण: What is constexpr?

cpp
// Include std::cout and std::cin
#include <iostream>

constexpr int square(int x) {
	// Send x * x back to the caller
	return x * x;
}

// Program execution starts in main()
int main() {
	constexpr int result = square(5);
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

constexpr Functions

एक constexpr function एक normal function की तरह लिखा जाता है, लेकिन जब इसे pass किया हर argument खुद एक compile-time constant हो, compiler compilation के दौरान पूरी function body evaluate करता है और result सीधे substitute कर देता है -- अगर कोई argument एक compile-time constant न हो, यह बस runtime पर एक normal function की तरह चलने पर fallback कर देता है।

उदाहरण: constexpr Functions

cpp
// Include std::cout and std::cin
#include <iostream>

constexpr int factorial(int n) {
	return (n <= 1) ? 1 : n * factorial(n - 1);
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << factorial(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

constexpr बनाम const

const सिर्फ promise करता है कि initialization के बाद एक variable की value नहीं बदलेगी -- वह initialization अभी भी runtime पर हो सकता है, user input जैसी किसी चीज़ पर based। constexpr इससे आगे जाता है, यह require करते हुए कि value compile time पर पूरी तरह computable हो, जो एक stronger और ज़्यादा restrictive guarantee है।

उदाहरण: constexpr vs. const

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare runtimeInput and set it to 5
	int runtimeInput = 5;
	// Declare a and set it to runtimeInput
	const int a = runtimeInput;
	constexpr int b = 10;
	// Print to the console with cout
	std::cout << a << " " << b << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

constexpr Constructors

एक constexpr constructor एक पूरे class object -- सिर्फ एक single value नहीं -- को compilation के दौरान पूरी तरह construct होने देता है, बशर्ते हर member खुद compile-time-constant expressions से initialize हो।

यह आपको zero runtime cost के साथ पूरी lookup tables या configuration objects बनाने देता है।

उदाहरण: constexpr Constructors

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
	int x, y;
	constexpr Point(int px, int py) : x(px), y(py) {}
};

// Program execution starts in main()
int main() {
	constexpr Point p(3, 4);
	// Print to the console with cout
	std::cout << p.x << "," << p.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Performance Gains

computation को runtime से compile time पर move करना मतलब CPU program actually चलते हुए इस पर कभी cycles खर्च नहीं करता -- सिर्फ constants पर depend करने वाले math के लिए (जैसे एक fixed-size buffer length या एक mathematical table), constexpr effectively उस computation को runtime पर free बना देता है।

उदाहरण: Performance Gains

cpp
// Include std::cout and std::cin
#include <iostream>

constexpr int bufferSize = 10 * 10;

// Program execution starts in main()
int main() {
	int buffer[bufferSize];
	// Print to the console with cout
	std::cout << bufferSize << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक constexpr variable में एक non-constant value इस्तेमाल करना, जो compile नहीं होता।
  2. const (runtime constant) को constexpr (compile-time constant) से confuse करना।
  3. एक constexpr initializer के अंदर एक non-constexpr function call करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.