Errors, bugs, questions - page 2860

 
Is this correct compiler behaviour?
class A
{
public:
  static int i;
  
  static int f()
  {
    A::i = 123;
    
    return(A::i);
  }
};

static int A::i = A::f();

void OnStart()
{
  Alert(A::i); // 123
}
It seems that A::i is not created when the A::f() method is called, which calls A::i.
 
fxsaber:
Is this correct compiler behavior? It seems that A::i is not created at the moment of calling the A::f() method calling A::i.

What is meant by "created" ?

static is just a way to create a rake to hide a global variable or function, limiting the visibility of a function or class

The memory for a global variable always exists.
And yes, for global variables the initialization order is very important (I mean, avoid accessing the variable before it is initialized)
This example worked...

Документация по MQL5: Основы языка / Переменные / Создание и уничтожение объектов
Документация по MQL5: Основы языка / Переменные / Создание и уничтожение объектов
  • www.mql5.com
После загрузки на исполнение mql5-программы каждой переменной выделяется память в соответствие с типом переменной. Переменные делятся на два типа по уровню доступа - глобальные переменные и локальные переменные, и по классам памяти: входные параметры mql5-программы, статические и автоматические. Каждая переменная при необходимости...
 
Ilyas:

In the example above it worked...

Are you going to change the compiler at this point? Personally, I'd like to keep it the way it is.

 
fxsaber:

Will you change the compiler at this point? Personally, I'd like to keep everything as it is.

We are not planning to change the behaviour.

But there is a deferred task of detecting errors of access to global variables (before initialization).
If we implement this feature, we'll get a warning when compiling the above code: the A::f() function is used to initialize A::i, which accesses the A::i variable being initialized.

Документация по MQL5: Основы языка / Переменные / Глобальные переменные
Документация по MQL5: Основы языка / Переменные / Глобальные переменные
  • www.mql5.com
Глобальные переменные создаются путем размещения их объявлений вне описания какой-либо функции. Глобальные переменные определяются на том же уровне, что и функции, т. е. не локальны ни в каком блоке. Область видимости глобальных переменных - вся программа, глобальные переменные доступны из всех функций, определенных в программе...
 
Ilyas:

If we implement the feature, you will get a warning message when compiling the above code: the A::f() function is used to initialize A::i, which accesses the A::i variable being initialized.

Thank you!

 
Ilyas:

We do not plan to change the behaviour

Then it contradicts your concept that a variable is considered declared when the declaration is complete. Why did you invent it in the first place? If it's like that in one case and different in another case

int i = i; //Error: 'i' - undeclared identifier
A good language has uniform rules, not the other way round
 
A100:

Then it contradicts your own concept that a variable is considered declared when the declaration is complete. Why did you invent it in the first place? If it is so in one case and different in another

A good language has uniform rules, not the other way round

I'm a total ignoramus, but I wouldn't have thought of it (int i = i) even if I had drunk too much....... 8(

 
Сергей Таболин:

I'm a total ignoramus, but I wouldn't have thought of this (int i = i) even if I had drunk too much....... 8(

And what is the principal difference from the original example? Remove unnecessary things and you'll get it:

int i = i = 123; //Error: 'i' - undeclared identifier

Only there are a lot of strings there, and only one here.

If there is no compilation error there shouldn't be one here too (and vice versa), otherwise it's a mess

 
A100:

Then it contradicts your own concept that a variable is considered declared when the declaration is complete. Why did you invent it in the first place? If it is so in one case and different in another

A good language has uniform rules, not the other way around
In this case it's OK.

The point is that you can get undefined behaviour in other, trickier conditions. Self-shots in C++ are implementable.
 
A100:

What is the fundamental difference from the original example? Take away the extra stuff and you've got it:

Only there are a lot of lines there, and only one here

If there's no compilation error there shouldn't be one here too (and vice versa), otherwise it's a mess.

You're wrong, it's not the same thing.

Description of a static variable in a class is its pre-definition (similar to pre-definition of a function or class), but location, in fact, just tells the compiler where the variable will be stored in memory and when it should be initialized

Reason: