logo

Crowdly

Browser

Add to Chrome

Just as in C, the type qualifier const  is used in C++ to declare and initializ...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

Just as in C, the type qualifier const is used in C++ to declare and initialize a read-only variable whose value cannot be changed during program execution. Therefore, the following code fragment will be flagged as a compile-time error:

int const value {10};

std::cout << "value: " << value << "\n";

value = 20; // error: assigning a new value to a const variable

std::cout << "value: " << value << "\n";

In both languages, the rule is that a const variable must be initialized during its definition. This makes sense since if a const variable's value cannot be changed during its lifetime, it can only be given a value when its lifetime begins. Therefore, the following code will be flagged as a compile-time error:

int const value; // error: const variable is not initialized during its definition

value = 20;

A constant expression is an expression whose value can be evaluated at compile-time and whose value cannot be changed during program execution. A literal value such as and is a constant expression. It may seem strange but a const variable need not be a constant expression. As an example:

void foo(int n) {

double pi = 3.14285714; // 3.14285714 is a constant expression

const int size = n; // not a constant expression

// other code here ...

}

literal is a constant expression while read-only variable size is not a constant expression since its value is only known at run-time.

Additionally in C++, a const variable initialized from a constant expression is also a constant expression. As an example:

void foo(int n) {

const double pi = 3.14285714; // constant expression

const int size = n; // not a constant expression

// other code here ...

}

read-only variable pi is a constant expression since it is initialized by a literal.

The following C++ code fragment

int const degree{4};

int poly[degree] {11,12,13,14};

establishes two levels of constancy for variable degree:

  • variable degree cannot be changed during program execution [just as in C], and
  • variable degree is a constant expression and can therefore be used to specify the size of a static array [unlike in C].

Compare the following C program's behavior:

#include <stdio.h>

int main(void) {

int const value = 4;

int array[value] = {1, 2, 3, 4};

printf("%d\n", array[1]);

return 0;

}

with the behavior of this C++ program

#include <iostream>

int main() {

int const value {4};

int array[value] {1, 2, 3, 4};

std::cout << array[1] << "\n";

}

to deduce that a const variable initialized from a constant expression [such as const variable value] is also a constant expression only in C++ code but not in C code.

Clearly, a constant expression has several advantages:

  1. A constant expression can be evaluated at compile-time to save from the CPU cycles required to evaluate the expression at run-time.
  2. A constant expression provides the compiler deep insight into the code so that the compiler can potentially determine additional optimizations.
  3. A constant expression makes the code implicitly thread safe which is difficult to achieve in multi-threaded code.

Whether a given variable or expression is a constant expression depends on the types and initializers:

int square(int n) {

return n*n;

}

int main() {

int const max_files{20}; // max_files is a constant expression

int const limit {max_files}; // limit is a constant expression

int size{27}; // size is not a constant expression

int const sq_size{square(2)}; // sq_size is not a constant expression

// because the value of the initializer is not known until run-time

int array[square(2)]{1,2,3,4}; // illegal because square(2) is not a constant expression

std::cout << array[2] << "\n";

}

Since C+11, the standard has introduced a new keyword that allows programmers to specify that the value of a variable [such as size] or an expression [such square(2)] must be evaluated at compile-time. This keyword when used as a specifier for function square will enable the const variable sq_size to be a constant expression and thus allow the compiler to define the array array with size .

Read Section of the textbook to learn what this C++ keyword is and then write the exact keyword as your answer.

More questions like this

Want instant access to all verified answers on distance3.sg.digipen.edu?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome