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
350
votes
7 answers

Are variables declared with let or const hoisted?

I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected... console.log(typeof name); // undefined var name = "John"; ...variables declared with let or const seem to have some problems…
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
342
votes
5 answers

Proper use of const for defining functions

Are there any limits to what types of values can be set using const in JavaScript, and in particular, functions? Is this valid? Granted it does work, but is it considered bad practice for any reason? const doSomething = () => { ... } Should all…
David Sinclair
  • 4,187
  • 3
  • 17
  • 12
314
votes
2 answers

What does the constant 0.0039215689 represent?

I keep seeing this constant pop up in various graphics header files 0.0039215689 It seems to have something to do with color maybe? Here is the first hit on Google: void RDP_G_SETFOGCOLOR(void) { Gfx.FogColor.R = _SHIFTR(w1, 24, 8) *…
crush
  • 16,713
  • 9
  • 59
  • 100
307
votes
21 answers

How do I remove code duplication between similar const and non-const member functions?

Let's say I have the following class X where I want to return access to an internal member: class Z { // details }; class X { std::vector vecZ; public: Z& Z(size_t index) { // massive amounts of code for validating…
Kevin
  • 25,207
  • 17
  • 54
  • 57
266
votes
9 answers

Difference between char* and const char*?

What's the difference between char* name which points to a constant string literal, and const char* name
Iceman
  • 4,202
  • 7
  • 26
  • 39
265
votes
8 answers

Constant pointer vs Pointer to constant

I want to know the difference between const int* ptr; and int * const ptr; and how it works. It is pretty difficult for me to understand or keep remember this. Please help.
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
261
votes
5 answers

Declare a constant array

I have tried: const ascii = "abcdefghijklmnopqrstuvwxyz" const letter_goodness []float32 = { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077,.0402,.0241,.0675, .0751,.0193,.0009,.0599,.0633,.0906,.0276,…
ceth
  • 44,198
  • 62
  • 180
  • 289
256
votes
11 answers

'static const' vs. '#define'

Is it better to use static const variables than #define preprocessor? Or does it maybe depend on the context? What are advantages/disadvantages for each method?
Patrice Bernassola
  • 14,136
  • 6
  • 46
  • 59
255
votes
6 answers

Why is 0 < -0x80000000?

I have below a simple program: #include #define INT32_MIN (-0x80000000) int main(void) { long long bal = 0; if(bal < INT32_MIN ) { printf("Failed!!!"); } else { printf("Success!!!"); …
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
253
votes
11 answers

How come a non-const reference cannot bind to a temporary object?

Why is it not allowed to get non-const reference to a temporary object, which function getx() returns? Clearly, this is prohibited by C++ Standard but I am interested in the purpose of such restriction, not a reference to the standard. struct…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
238
votes
11 answers

Is it better in C++ to pass by value or pass by reference-to-const?

Is it better in C++ to pass by value or pass by reference-to-const? I am wondering which is better practice. I realize that pass by reference-to-const should provide for better performance in the program because you are not making a copy of the…
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
238
votes
17 answers

How to select multiple rows filled with constants?

Selecting constants without referring to a table is perfectly legal in an SQL statement: SELECT 1, 2, 3 The result set that the latter returns is a single row containing the values. I was wondering if there is a way to select multiple rows at once…
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
235
votes
13 answers

Ruby on Rails: Where to define global constants?

I'm just getting started with my first Ruby on Rails webapp. I've got a bunch of different models, views, controllers, and so on. I'm wanting to find a good place to stick definitions of truly global constants, that apply across my whole app. In…
AlexC
  • 3,610
  • 3
  • 26
  • 26
210
votes
4 answers

Why isn't String.Empty a constant?

In .NET why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision.
travis
  • 35,751
  • 21
  • 71
  • 94
210
votes
13 answers

Why can I change a constant object in JavaScript?

I know that ES6 is not standardized yet, but a lot of browsers currently support const keyword in JS. In spec, it is written that: The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this,…
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753