Questions tagged [constants]

Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant. A const-qualified data storage area (object, field, variable, parameter) is one that "never changes", thus allowing extra code generator optimizations and additional static checking of program correctness.

In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages.

The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what they can do.

In addition, a method can be declared as const. In this case, the this pointer inside such a method is of const ThisClass* const type rather than of ThisClass* const type. This means that non-const methods for this object cannot be called from inside such a method, nor can member variables be modified. In C++, a member variable can be declared as mutable, indicating that this restriction does not apply to it. In some cases, this can be useful, for example with caching, reference counting, and data synchronization. In these cases, the logical meaning (state) of the object is unchanged, but the object is not physically constant since its bitwise representation may change.

The C++11 standard adds additional notes on the meaning of const when used in the Standard Library with respect to thread access and possible modifications of those const objects.

More about const-correctness here.

10286 questions
208
votes
5 answers

How can I get all constants of a type by reflection?

How can I get all constants of any type using reflection?
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
198
votes
4 answers

How to sort with a lambda?

sort(mMyClassVector.begin(), mMyClassVector.end(), [](const MyClass & a, const MyClass & b) { return a.mProperty > b.mProperty; }); I'd like to use a lambda function to sort custom classes in place of binding an instance method. However,…
BTR
  • 4,880
  • 4
  • 24
  • 21
198
votes
15 answers

Include constant in string without concatenating

Is there a way in PHP to include a constant in a string without concatenating? define('MY_CONSTANT', 42); echo "This is my constant: MY_CONSTANT";
Brian
  • 26,662
  • 52
  • 135
  • 170
197
votes
2 answers

What does the PHP error message "Notice: Use of undefined constant" mean?

PHP is writing this error in the logs: "Notice: Use of undefined constant". Error in logs: PHP Notice: Use of undefined constant department - assumed 'department' (line 5) PHP Notice: Use of undefined constant name - assumed 'name' (line 6) PHP…
Nik
  • 2,424
  • 3
  • 18
  • 16
196
votes
3 answers

What is the temporal dead zone?

I've heard that accessing let and const values before they are initialized can cause a ReferenceError because of something called the temporal dead zone. What is the temporal dead zone, how does it relate to scope and hoisting, and in what…
joews
  • 29,767
  • 10
  • 79
  • 91
195
votes
3 answers

Go naming conventions for const

I'm trying to determine whether there is a naming convention for the names of const in Golang. I personally would tend to follow the C style and write them in upper case, but I haven't found anything on this page…
LtWorf
  • 7,286
  • 6
  • 31
  • 45
192
votes
7 answers

C++: const reference, before vs after type-specifier

What is the difference between the arguments in: int foo1(const Fred &arg) { ... } and int foo2(Fred const &arg) { ... } ? I don't see this case covered in the parashift FAQ.
eisbaw
  • 2,678
  • 2
  • 19
  • 18
190
votes
8 answers

PHP 5: const vs static

In PHP 5, what is the difference between using const and static? When is each appropriate? And what role does public, protected and private play - if any?
Chris Jacob
  • 11,878
  • 7
  • 47
  • 42
189
votes
9 answers

Why are both "const T" and "T const" valid, and which one should you use?

To start you probably know that const can be used to make either an object's data or a pointer not modifiable or both. const Object* obj; // can't change data Object* const obj; // can't change pointer const Object* const obj; // can't change data…
AJG85
  • 15,849
  • 13
  • 42
  • 50
184
votes
17 answers

How can I get a resource content from a static context?

I want to read strings from an xml file before I do much of anything else like setText on widgets, so how can I do that without an activity object to call getResources() on?
lost baby
  • 3,178
  • 4
  • 32
  • 53
183
votes
4 answers

Class constants in python

In python, I want a class to have some "constants" (practically, variables) which will be common in all subclasses. Is there a way to do it with friendly syntax? Right now I use: class Animal: SIZES=["Huge","Big","Medium","Small"] class…
Idan
  • 5,365
  • 5
  • 24
  • 28
176
votes
7 answers

What is the difference between const_iterator and non-const iterator in the C++ STL?

What is the difference between a const_iterator and an iterator and where would you use one over the other?
Konrad
  • 39,751
  • 32
  • 78
  • 114
175
votes
10 answers

How can I specify a [DllImport] path at runtime?

In fact, I got a C++ (working) DLL that I want to import into my C# project to call it's functions. It does work when I specify the full path to the DLL, like this : string str =…
Jsncrdnl
  • 3,005
  • 5
  • 28
  • 43
171
votes
4 answers

Purpose of returning by const value?

What is the purpose of the const in this? const Object myFunc(){ return myObject; } I've just started reading Effective C++ and Item 3 advocates this and a Google search picks up similar suggestions but also counterexamples. I can't see how…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
168
votes
4 answers

Accessing a class's constants

When I have the following: class Foo CONSTANT_NAME = ["a", "b", "c"] ... end Is there a way to access with Foo::CONSTANT_NAME or do I have to make a class method to access the value?
Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114