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
7
votes
1 answer

Why does WSAStringToAddress take a non-const AddressString?

The Windows documentation for WSAStringToAddress states: INT WSAAPI WSAStringToAddress( _In_ LPTSTR AddressString, _In_ INT AddressFamily, _In_opt_ LPWSAPROTOCOL_INFO lpProtocolInfo, _Out_ LPSOCKADDR …
jww
  • 97,681
  • 90
  • 411
  • 885
7
votes
2 answers

Why is `exists` modifying my constant?

The exists function can unexpectedly autovivify entries in hashes. What surprises me is that this behavior carries over to constants as well: use strict; use warnings; use Data::Dump 'dump'; use constant data => { 'foo' => { …
Zaid
  • 36,680
  • 16
  • 86
  • 155
7
votes
2 answers

const propagation to std::array of pointers

Why is std::array's data type being instantiated differently here using T = const int *; std::array x = { &a, &b, &c, &d }; // name: class std::array x[0] = &c; // OK : non-constant pointer *x[0] = c; // Error : constant…
Judge
  • 372
  • 2
  • 8
7
votes
3 answers

Why is const unnecessary in function declarations in header files for parameters passed by value?

I was recently reading about the usage of const keyword as function arguments in C and the way to use that has been mentioned in When and for what purposes should the const keyword be used in C for variables and been accepted as correct answer. In…
Swapnil
  • 1,870
  • 2
  • 23
  • 48
7
votes
3 answers

OpenCV-Python cv2.CV_CAP_PROP_POS_FRAMES error

Currently, I am using opencv 3.1.0, and I encountered the following error when executing the following code: post_frame = cap.get(cv2.CV_CAP_PROP_POS_FRAMES) I got the following error Message: File "videoOperation.py", line 37, in pos_frame =…
shady
  • 455
  • 1
  • 7
  • 18
7
votes
2 answers

Creating a constant dictionary object

I would like to accomplish something like what is being done in this post: Constants in Objective-C however, i would like to construct an NSDictionary. if i do something like: constants.h extern NSArray *const mFooKeys; extern NSArray *const…
griotspeak
  • 13,022
  • 13
  • 43
  • 54
7
votes
1 answer

Declaring private constants outside of a class in Swift

When creating private constants in Swift it is possible to declare them within a class, final class SomeClass: NSObject { private let someFloat:CGFloat = 12 } as well as outside of a class. private let someFloat:CGFloat = 12 final class…
bmjohns
  • 6,344
  • 1
  • 33
  • 39
7
votes
6 answers

Ways to change const value in c++

const value is often used in programming. It gives programmer security that the value will not change in future. Unfortunately, this is not fully enforced. As a consequence it sometimes causes subtle bugs that are very hard to decipher. As an…
Existent
  • 697
  • 1
  • 7
  • 14
7
votes
1 answer

Does removing const from a function return type break ABI?

Assume I have a library that declares a function returning const type: class Foo { ... }; const Foo makeFoo(); Now I want to remove const from makeFoo() return type (See my previous question). I can remove const both from the header and the cpp…
Petr
  • 9,812
  • 1
  • 28
  • 52
7
votes
1 answer

Is it okay to define the actual argument as `const int` but declare it as just `int` in the header?

See the following program. #include void f(int a); int main() { f(10); return 0; } void f(const int a) { /* a = 20; */ /* Want to avoid accidental modification of a. */ printf("%d\n", a); } In this program the declaration of…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
7
votes
3 answers

Enum.GetName() As Constant property

I have been working in C# for about 8 months so forgive me if this is dumb... I have an enum that I will need the string value several times in a class. So I want to use Enum.GetName() to set it to a string variable which is no problem. I just do it…
DVS
  • 783
  • 7
  • 25
7
votes
1 answer

Why is my const array stored on the stack instead of the text section?

I'm implementing a encryption algorithm (for educational purposes) and I noticed something strange. Part of the algorithm uses s-boxes to do substitution, so I allocated const arrays to use as a lookup table like this: const unsigned char…
Robert Stiffler
  • 675
  • 3
  • 10
7
votes
1 answer

static, constexpr, const - what do they mean when all used together?

I'm totally frustrated with these specifiers because I understand what they do when they're by themselves but I find them hard to understand when they're used with each other. For example, some code in the wild contained - namespace{ static…
hg_git
  • 2,884
  • 6
  • 24
  • 43
7
votes
1 answer

Defining multiple constant variables in clojure

I'm trying to define several constant variables in clojure. Is there a way to define all of them in one def statement? Or must I define each one separately? In any programming language (C++ Java) you may expect to be able to do the following …
Lucas Rudd
  • 385
  • 1
  • 13
7
votes
3 answers

How to set a const int to maximum in C++?

I have a static const member and would like to set it to the maximum integer. I'm trying the following: const static int MY_VALUE = std::numeric_limits::max(); But get the following error: error: in-class initializer for static data member is…
Cisplatin
  • 2,860
  • 3
  • 36
  • 56