C++ का constexpr
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
constexpr बनाम const
const सिर्फ promise करता है कि initialization के बाद एक variable की value नहीं बदलेगी -- वह initialization अभी भी runtime पर हो सकता है, user input जैसी किसी चीज़ पर based। constexpr इससे आगे जाता है, यह require करते हुए कि value compile time पर पूरी तरह computable हो, जो एक stronger और ज़्यादा restrictive guarantee है।
उदाहरण: constexpr vs. const
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक
constexprvariable में एक non-constant value इस्तेमाल करना, जो compile नहीं होता। const(runtime constant) कोconstexpr(compile-time constant) से confuse करना।- एक
constexprinitializer के अंदर एक non-constexpr function call करना।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: