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

candidate function not viable: 1st argument ('const Node *') would lose const qualifier

I am writing a DiGraph (directed graph) class with the c++ built in unordered_map> data structure, where Node and Edge are two structs I defined myself. And in the class I wrote a containsNode() method to search if a Node…
Kevin King
  • 71
  • 1
  • 1
  • 2
7
votes
2 answers

How to create a file of constants in Javascript?

Is there a way to create a file of constants in JavaScript, which I can reference and then use? What I am looking for is something like this: Constants.js: var Phones = { Nokia: 1, Samsung: 2 } Then, in another JavaScript…
TheRifle
  • 71
  • 1
  • 2
7
votes
4 answers

When should constants be defined in their own files?

I've noticed some projects like to store constants in their own file, i.e constants used globally and in the main program loop might clutter the main file so perhaps they look to place them elsewhere and then reference/import file/class. I…
insidesin
  • 735
  • 2
  • 8
  • 26
7
votes
2 answers

How to inject service to angular constant

I would like to define a constant which use $locale service. Constants are objects, so I can't inject it as parameter as in case of controller. How can I use it? angular.module('app').constant('SOME_CONSTANT', { 'LOCALE': $locale.id.slice(0,…
7
votes
2 answers

Setter params final in Java

I have always been programming in java, and recently i started learning some c++. In C++ it is conventional to set setter params as const, why don't we see this as much in java ? I mean are there any disadvantages to creating a setter like…
Koen Demonie
  • 539
  • 1
  • 7
  • 24
7
votes
2 answers

const methods in golang?

In golang, often you want to declare a pointer-type associated method, because you don't want to copy a huge struct around: func (a *HugeStructType) AMethod() { .... } In C++, when I wanted to make such a method, but guarantee that it didn't…
djhaskin987
  • 9,741
  • 4
  • 50
  • 86
7
votes
3 answers

Where should Constant variables live in Symfony2 bundle

Maybe some will see that the question silly, however it is not. I am asking where should Constant should live in Symfony2 Bundle, I used in other frameworks to create my constants variable in Models, however since in Symfony2 I am using Entity, I am…
Dahab
  • 518
  • 1
  • 5
  • 23
7
votes
3 answers

Is there anything wrong with using static const structs to limit name collisions of constants in C?

For example, if I were to create a hierarchical static const struct like this in a header (.h) file: static const struct { struct { char STATIC /* = 0 */; char DYNAMIC /* = 1 */; } ALLOCATION; struct { char TABLE…
Matt
  • 21,026
  • 18
  • 63
  • 115
7
votes
1 answer

What is the relevance of this statement in 7.1.6.1/1 in the C++ Standard?

7.1.6.1/1 contains the following statement (emphasis mine): There are two cv-qualifiers, const and volatile. If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty. What is the…
Wake up Brazil
  • 3,421
  • 12
  • 19
7
votes
1 answer

C++11 binding rules for const &&

Many people do not know that const rvalue references are part of the C++11 language. This blog post discusses them but appears to be mistaken regarding the binding rules. Quoting the blog: struct s {}; void f ( s&); // #1 void f (const s&); …
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
7
votes
3 answers

What's the best way to store URLs, or URL domains, in AngularJS app?

My AngularJS app makes calls to an API which is currently hosted at one service, but was previously hosted at a different one, and in the near future is likely to be hosted yet somewhere else. The URL is regularly changing. For example…
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
7
votes
3 answers

Declaring a global variable `extern const int` in header but only `int` in source file

I was experimenting with GCC and found out that you can declare external variables const in header files but keep them mutable in implementation files. EDIT: This does actually not work. The only reason I got my test code to compile was because I…
wefwefa3
  • 3,872
  • 2
  • 29
  • 51
7
votes
1 answer

Why typecasting an integer literal to a pointer value results in a non-const expression?

I'm trying to write a struct to calculate the pointer offset between a base and a derived class as a constant expression in C++03. The code is as follows: template struct OffsetToBase { …
Lingxi
  • 14,579
  • 2
  • 37
  • 93
7
votes
6 answers

Why is passing a string literal into a char* argument only sometimes a compiler error?

I'm working in a C, and C++ program. We used to be compiling without the make-strings-writable option. But that was getting a bunch of warnings, so I turned it off. Then I got a whole bunch of errors of the form "Cannot convert const char* to char*…
Brian Postow
  • 11,709
  • 17
  • 81
  • 125
7
votes
1 answer

Dividing a const by a generic in Rust

I have a struct Vec3, how can I implement the following method ? impl Not> for Vec3 { fn not(&self) -> Vec3 { *self * (1.0 / (*self % *self).sqrt()) } } error: mismatched types: expected _,…
Shengis
  • 176
  • 1
  • 12
1 2 3
99
100