C++ language has always had the concept of constant expressions.
These are expressions such as 3+4 that will always yield the same
results, at compile time and at run time. Constant expressions are
optimization opportunities for compilers, or compilers frequently
execute them at compile time or hardcode the results in the program.
Also, there are a number of places where the C++ language specification
requires the use of constant expressions. Defining an array requires a
constant expression, or enumerator values must be constant expressions.
However, constant expressions have always ended whenever a function call and object constructor was encountered. So a piece of code as very simple as this is illegal:
Into get five () {return 5;}
Into some value [get five () + 7]; // Create an array of 12 integers. Ill-formed C++ language
This was not legal in C++03, because get five () + 7 is not a constant expression. A C++03 language compiler has no way of knowing if get five () actually is constant at runtime. In theory, this function could affect a global variable, and call other non-runtime constant functions, etc.
C++ assignment help 11 language introduced the keyword constexpr, which allows the user to guarantee that a function or object constructor is a compile-time constant [6]. The above example can be rewritten as follows:
constexpr int get_five() {return 5;}
into som _value[get _five() + 7]; // Create an array of 12 integers. Legal C++11
This is allows the compiler to understand, or verify, that get _five is a compile-time constant.
The use of constexpr on a function imposes some limitations on what that function can do. First, the function must have a non-void return type. 2nd, the function body cannot declare variables or define new types. 3rd, the body may contain only declarations, null statements and only single return statement. There must exist argument values such that, after argument substitution, the expression in the return statement produces a constant expression.
Prior to C++ C++ assignment help and language, the values of variables could be used in constant expressions only if the variables are declared const, have an initialize which is a constant expression, or are of integral or enumeration type. C++11 remove the restriction that the variables very must be of integral or enumeration type if they are defined with the constexpr keyword:
constexpr double acceleration_due_to_gravity = 9.8;
constexpr double moon_gravity = acceleration_due_to_gravity / 6.0;
Such data type variables are implicitly const, and must have an initializer which must be a constant expression.
related links: C, C++ Assignment Help
However, constant expressions have always ended whenever a function call and object constructor was encountered. So a piece of code as very simple as this is illegal:
Into get five () {return 5;}
Into some value [get five () + 7]; // Create an array of 12 integers. Ill-formed C++ language
This was not legal in C++03, because get five () + 7 is not a constant expression. A C++03 language compiler has no way of knowing if get five () actually is constant at runtime. In theory, this function could affect a global variable, and call other non-runtime constant functions, etc.
C++ assignment help 11 language introduced the keyword constexpr, which allows the user to guarantee that a function or object constructor is a compile-time constant [6]. The above example can be rewritten as follows:
constexpr int get_five() {return 5;}
into som _value[get _five() + 7]; // Create an array of 12 integers. Legal C++11
This is allows the compiler to understand, or verify, that get _five is a compile-time constant.
The use of constexpr on a function imposes some limitations on what that function can do. First, the function must have a non-void return type. 2nd, the function body cannot declare variables or define new types. 3rd, the body may contain only declarations, null statements and only single return statement. There must exist argument values such that, after argument substitution, the expression in the return statement produces a constant expression.
Prior to C++ C++ assignment help and language, the values of variables could be used in constant expressions only if the variables are declared const, have an initialize which is a constant expression, or are of integral or enumeration type. C++11 remove the restriction that the variables very must be of integral or enumeration type if they are defined with the constexpr keyword:
constexpr double acceleration_due_to_gravity = 9.8;
constexpr double moon_gravity = acceleration_due_to_gravity / 6.0;
Such data type variables are implicitly const, and must have an initializer which must be a constant expression.
related links: C, C++ Assignment Help
No comments:
Post a Comment