Evgenii Legotckoi
Evgenii LegotckoiJune 5, 2017, 3:33 a.m.

C++ - Tutorial 003. Constants

Content

C =++ supports two notations of immutability:

  1. const - which implies that the value will not change. First of all, this is used to specify interfaces, for data that is passed to functions and methods so that they are not afraid that they will be changed. The compiler monitors the presence of the const qualifier;
  2. constexp - which involves calculating the constant at compile time. Used to store data in memory where they will not be damaged, and to improve performance.

For example:

const int dmv = 17;                      // Constant with the name dmv
int var = 17;                            // Var is not a constant
constexpr double max1 = 1.4∗square(dmv); // OK, since square (17) is a constant expression
constexpr double max2 = 1.4∗square(var); // Error, because var is not a constant
const double max3 = 1.4∗square(var);     // OK, because the expression can be evaluated in runtime
double sum(const vector<double>&);       // sum will not modify its arguments
vector<double> v {1.2, 3.4, 4.5};        // v is not a constant
const double s1 = sum(v);                // OK, it will be calculated in runtime
constexpr double s2 = sum(v);            // Error, sum (v) is not a constant expression.

In order for the function to be used in a constant expression, that is, it was computed by the compiler, it is necessary to define it with the constexpr qualifier.

constexpr double square(double x) { return x∗x; }

The constexpr function must be simple enough to compute by the compiler, and also return the computed value. Constexpr functions can be called by non-constants arguments in the context of which constant expressions are not required.

const

Objects with the const qualifier can not be changed, and must also be initialized.

const int model = 90;           // model is a constant
const int v[] = { 1, 2, 3, 4 }; // v[i] is a constant
const int x;                    // Error: value not initialized

Since objects with const qualifiers can not be changed, the following code will be incorrect:

void f()
{
    model = 200; // Error: model is a constant
    v[2] = 3;    // Error: v[i] is a constant
}

Note that const changes the type of the object, not an indication of how the variable should be assigned. const limits the way the object works.

void g(const X∗ p)
{
    // Can not change p here
}

void h()
{
    X val;  // But we can modify the value here
    g(&val);
    // ...
}

When using a pointer, two objects are involved: the pointer itself and the object pointed to. A prefix declaration of a pointer with const makes the object constant, not a pointer. To declare const as the pointer itself, and not the object to which it points, you must place const after the pointer character. For example:

void f1(char∗ p)
{
    char s[] = "Gorm";

    const char∗ pc = s;        // Pointer to constant
    pc[3] = 'g';               // Error: the value of the object is a constant
    pc = p;                    // OK

    char ∗const cp = s;        // Constant pointer
    cp[3] = 'a';               // OK
    cp = p;                    // Error: cp is a constant

    const char ∗const cpc = s; // Constant pointer to a constant object
    cpc[3] = 'a';              // Error: the object is a constant
    cpc = p;                   // Error: pointer is a constant
}

The location of const relative to the base type is not important, since there is no data type const . Principle is the position of const relative to the symbol . Therefore, the following entries are possible:

char ∗const cp;  // const pointer to char
char const∗ pc;  // pointer to const char
const char∗ pc2; // pointer to const char

An object that is a constant when accessed through one pointer can be changed when accessed in other ways. This is especially useful for function arguments. Declaring a pointer argument as a const, a function is not allowed to change the object it points to. For example:

const char∗ strchr(const char∗ p, char c);
char∗ strchr(char∗ p, char c);

The first version is used for strings whose elements should not be changed by the function and returns a pointer to const that does not allow changing the result. The second version is used for mutable lines.

You can assign a non-constant variable address to a pointer to a constant, because it can not do any harm. However, you can not assign a constant address to a non-constant pointer, since this will change the value of the object. For example:

void f4()
{
    int a = 1;
    const int c = 2;
    const int∗ p1 = &c; // OK
    const int∗ p2 = &a; // OK
    int∗ p3 = &c;       // Error: initialization int* with const int*
    ∗p3 = 7;            // Attempt to change the value of c
}

constexpr

A constant expression is an expression that is evaluated at compile time. Constant expressions can not use values and variables that are not known at compile time.

There are many reasons why someone might need a named constant, not a letter or value stored in a variable:

  1. Named constants simplify understanding and code support.
  2. A variable can be changed (so we must be more careful in our reasoning than with a constant).
  3. The language requires constant expressions for array sizes, case labels, and template value arguments.
  4. Embedded programmers like to put immutable data in a persistent storage device. Because read-only memory is cheaper than dynamic memory (in terms of cost and energy consumption) and often more numerous. In addition, the data in the permanent memory is protected from most system failures.
  5. If initialization is performed at compile time, there can not be any discrepancies in the multithreaded program.
  6. Performing compilation at the compilation stage improves program performance.

The value of constexpr is evaluated at compile time, and if it can not be computed, the compiler will generate an error.

int x1 = 7;
constexpr int x2 = 7;
constexpr int x3 = x1;     // Error: initializer is not a constant expression
constexpr int x4 = x2;     // OK

void f()
{
    constexpr int y3 = x1; // Error: initializer is not a constant expression
    constexpr int y4 = x2; // OK
    // ...
}

The possibilities of constant expressions are quite large, since it is possible to use integer data types, floating-point data, enums, and operators that do not change the values of variables (such as +,? And [] , but not = or ++ )

constexpr int isqrt_helper(int sq, int d, int a)
{
    return sq <= a ? isqrt_helper(sq+d,d+2,a) : d;
}

constexpr int isqrt(int x)
{
    return isqrt_helper(1,3,x)/2 − 1;
}

constexpr int s1 = isqrt(9);
constexpr int s2 = isqrt(1234);
We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

BlinCT
  • June 5, 2017, 4:55 a.m.

Отличное описание и примеры!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
e
  • ehot
  • March 31, 2024, 2:29 p.m.

C++ - Тест 003. Условия и циклы

  • Result:78points,
  • Rating points2
B

C++ - Test 002. Constants

  • Result:16points,
  • Rating points-10
B

C++ - Test 001. The first program and data types

  • Result:46points,
  • Rating points-6
Last comments
k
kmssrFeb. 8, 2024, 6:43 p.m.
Qt Linux - Lesson 001. Autorun Qt application under Linux как сделать автозапуск для флэтпака, который не даёт создавать файлы в ~/.config - вот это вопрос ))
Qt WinAPI - Lesson 007. Working with ICMP Ping in Qt Без строки #include <QRegularExpressionValidator> в заголовочном файле не работает валидатор.
EVA
EVADec. 25, 2023, 10:30 a.m.
Boost - static linking in CMake project under Windows Ошибка LNK1104 часто возникает, когда компоновщик не может найти или открыть файл библиотеки. В вашем случае, это файл libboost_locale-vc142-mt-gd-x64-1_74.lib из библиотеки Boost для C+…
J
JonnyJoDec. 25, 2023, 8:38 a.m.
Boost - static linking in CMake project under Windows Сделал всё по-как у вас, но выдаёт ошибку [build] LINK : fatal error LNK1104: не удается открыть файл "libboost_locale-vc142-mt-gd-x64-1_74.lib" Хоть убей, не могу понять в чём дел…
G
GvozdikDec. 18, 2023, 9:01 p.m.
Qt/C++ - Lesson 056. Connecting the Boost library in Qt for MinGW and MSVC compilers Для решения твой проблемы добавь в файл .pro строчку "LIBS += -lws2_32" она решит проблему , лично мне помогло.
Now discuss on the forum
a
a_vlasovApril 14, 2024, 6:41 a.m.
Мобильное приложение на C++Qt и бэкенд к нему на Django Rest Framework Евгений, добрый день! Такой вопрос. Верно ли следующее утверждение: Любое Android-приложение, написанное на Java/Kotlin чисто теоретически (пусть и с большими трудностями) можно написать и на C+…
Павел Дорофеев
Павел ДорофеевApril 14, 2024, 2:35 a.m.
QTableWidget с 2 заголовками Вот тут есть кастомный QTableView с многорядностью проект поддерживается, обращайтесь
f
fastrexApril 4, 2024, 4:47 a.m.
Вернуть старое поведение QComboBox, не менять индекс при resetModel Добрый день! У нас много проектов в которых используется QComboBox, в версии 5.5.1, когда модель испускает сигнал resetModel, currentIndex не менялся. В версии 5.15 при resetModel происходит try…
AC
Alexandru CodreanuJan. 19, 2024, 11:57 a.m.
QML Обнулить значения SpinBox Доброго времени суток, не могу разобраться с обнулением значение SpinBox находящего в делегате. import QtQuickimport QtQuick.ControlsWindow { width: 640 height: 480 visible: tr…

Follow us in social networks