When writing a method in class, I noticed that the compiler (VS 2015) recognizes const and CONST as different identifiers, why? Is there any difference? Why redefined const in CONST?

code example:

class A{ public: void a(CONST int& val);//Будет ругаться, что метод не описан! }; void A::a(const int& val){} 
  • Sample code do not want to add? - αλεχολυτ
  • @alexolut - added - Duracell
  • As you already answered c++ case sensitive (this is not a basic). You can not write CONST where you need const . mAiN , where you need main , etc. - αλεχολυτ

2 answers 2

The fact is that the source is often designed to compile in both C mode and C ++ (for example, this is the case for WinAPI header files).

To do this, you can declare CONST macro, which is expanded in const when compiling C ++, and an empty string when compiling C.

It is often used in reality.

Extract from a fresh WinAPI header file:

 #ifndef CONST #define CONST const #endif 

In your particular case it is better not to bother, and to define CONST :

 #define CONST const 

(well, or find some header file in which it is).


More on the topic: Why does Microsoft in WinApi create its macros for the definitions in the language?

  • const in c already from the antediluvian times there. - αλεχολυτ
  • @alexolut Not in MS VC :) - VladD
  • What revision of VC should be? rextester.com/EBO9385 - αλεχολυτ
  • @alexolut: Pretty old, I only have 2015, so I can't try. But Microsoft retains old definitions due to backward compatibility. - VladD
  • For fun, I installed MS Developer Studio 97 . Works great with const in c version. - αλεχολυτ

In general, C ++ is case-sensitive, so const is a keyword, and CONST is a regular identifier ...

 const int CONST = 1; 

fully compiled :)

  • there is also a very small chance that somewhere in the code there is something in the spirit of #define CONST const :) - StateItPrimitive
  • @StateItPrimitive He has - quite, but in Visual C ++ 2015 - there is definitely no such thing ... - Harry
  • @VladD thinks differently. - αλεχολυτ
  • one
    @alexolut Once again - yes, such a redefinition is in the Windows Kit, but in the compiler itself and its header files there is no such thing. In the initial formulation of the question there was no question that the client connects <windows.h> or whatever. There was a question about pure VC ++ 2015 - the compiler (VS 2015) recognizes const and CONST as different identifiers . Those. it was generally implied that he should NOT distinguish between them - which is absolutely nonsense. I replied to what was written :) - Harry
  • Everything is tied to each other, for example : If you uninstall components that other apps use, you'll run into problems. For example, Visual Studio also uses the Windows SDK. If you uninstall components that other apps use, you'll run into problems. For example, Visual Studio also uses the Windows SDK. . Anyway. I still have a lack of clarity - what version of vc const did not work in c , that it is still relevant to think about it in the context of modern development. - αλεχολυτ