Skip to main content

Questions tagged [const]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
4 votes
4 answers

Is const appropriate in this situation?

As an embedded firmware developer, I often write classes to represent/act as drivers for hardware on a microcontroller. These driver classes will usually contain pointers to hardware registers and ...
Patrick Wright's user avatar
0 votes
0 answers

Const correct interface for non-mutating function that returns mutable references into data structure

I very much want my code to be const correct and I'm struggling to get the following case right. Say we have a function F that takes a data structure D and computes an auxiliary data structure D'. F ...
chrysante's user avatar
  • 127
3 votes
1 answer

const correctness re: reference members

tl;dr is is a good idea for const methods to be able to mutate objects referenced as reference members? Imagine you have some kind of work queue that uses items that look like so: class Consumable { /...
Daniel McLaury's user avatar
1 vote
1 answer

What is the best way to handle the scenario in C++ where there are two methods that are identical, except one is const?

I am currently working on a program in which I encountered an issue (not for the first time) where I have two acessor methods for a data structure. The methods are identical, except one is const and ...
john01dav's user avatar
  • 889
81 votes
9 answers

How can I make a call with a boolean clearer? Boolean Trap

As noted by in the comments by @benjamin-gruenbaum this is called the Boolean trap: Say I have a function like this UpdateRow(var item, bool externalCall); and in my controller, that value for ...
Mario Garcia's user avatar
0 votes
1 answer

Does it make sense to iterate a ranged for loop using constant reference here?

I have the following code, and I was wondering if I'm optimally iterating through my ranged for loop: struct data_type { int a; int b; }; int main() { // Assume I have initialized a ...
Inertial Ignorance's user avatar
28 votes
7 answers

May a value of a constant be changed over time?

During development phase, there are certain variables which need to be the fixed in the same run, but may need be modified over time. For example a boolean to signal debug mode, so we do things in the ...
GregT's user avatar
  • 407
2 votes
2 answers

Method manipulates global variables, mark const?

I wrote a class ShaderWrapper in C++, that wraps around OpenGL shaders. In the constructor I generate the actually OpenGL shader object, but this method only returns me a handle/id of type int. The ...
Jakube's user avatar
  • 152
0 votes
1 answer

What support is there for "effectively-const" objects?

I'm using C++ concepts to ask the question because that's where it came up, but I'm asking about any language feature or tool that would somehow support this. Suppose I have a class that's a light ...
user avatar
-6 votes
6 answers

What would be generally accepted shorter version of `const`? [closed]

Now that we have const implemented javascript we can use it to declare variables. But unlike let or var it is 5-character long and I believe it is a big deal actually. Something that even slightly ...
shabunc's user avatar
  • 2,454
0 votes
2 answers

Is this a constant or a variable?

I was wondering if a value that is defined by the user at the start of a program, and not modified by the program, is considered a constant or a variable. I know that a constant is a word/letter that ...
AvahW's user avatar
  • 111
10 votes
5 answers

Should I be using const more in C++?

There are some things I am pretty strict about, but const has not been one of them. For example, I might make a local variable that I use three times in a function, that does not get changed, and yet ...
Anon's user avatar
  • 3,613
25 votes
5 answers

In C/C++, should I use 'const' in parameters and local variables when possible?

This question is inspired by a question about final in java. In C/C++, should I use const whenever possible? I know there is already a related question about using const in parameters. ...
ocomfd's user avatar
  • 5,712
23 votes
6 answers

Is readability a valid reason to not use const in (reference) parameters?

When writing some functions, I found a const keyword in parameters like this: void MyClass::myFunction(const MyObject& obj,const string& s1,const string& s2,const string& s3){ } ...
ggrr's user avatar
  • 5,783
5 votes
1 answer

How is it possible to program using only immutable / "read-only" variables?

I believe there are languages where all names with associated values cannot have their associated values changed. An example would be a language where all names behave like a does in the following C ...
Jackson Ray Hamilton's user avatar
2 votes
1 answer

Is it impossible to write proper, const correct callbacks in C?

In C, a non const object implicitly converts, without warning, to its const counterpart, so you can write a function, and call it as: void foo( const char *p ); char bar[] = "bar"; foo( bar ); ...
psusi's user avatar
  • 123
8 votes
4 answers

SQL Query and Java Constant Abuse?

I am currently charged with taking over a lot of code that is written with fields names placed into Java constant at the top of the file, and then the SQL queries constructed using string ...
AgilePro's user avatar
  • 312
2 votes
2 answers

Should constant values be in SHOUT_CASE when there is language support for them?

Referring primarily to here, it suggests that values which are constant in JavaScript (using the keyword const) should be named in SHOUT_CASE. I'm of the opinion though that mutability is much more ...
Dan's user avatar
  • 654
11 votes
4 answers

In C# Is using private constant strings better than using strings directly in the code? [closed]

Consider the following two cases: case 1: class A { private const string MyConst="Hello"; internal void CallMe() { System.Console.WriteLine(MyConst); } } Case2: class A { ...
GawdePrasad's user avatar
15 votes
2 answers

Const C++ DRY Strategies

For avoiding non-trivial C++ const related duplication, are there cases where const_cast would work but a private const function returning non-const wouldn't? In Scott Meyers' Effective C++ item 3, ...
JDiMatteo's user avatar
  • 375
0 votes
1 answer

Const and non-const methods, and possible mutable data member?

I'm working on a design and implementation change due to a bug. The bug fix requires us to modify a class object in a const method. I think the two options are: Have the non-const method cast this to ...
user avatar
4 votes
3 answers

Const means Thread-safe? [closed]

I was put onto this video: By Because I asked ...
Persixty's user avatar
  • 355
8 votes
3 answers

Passing parameters that need to be copied by value or const reference

I have a basic (mathematical) vector class, which in my opinion benefits from C++'s operator overloading. Vector-scalar operations are defined as self-modifying functions in the class itself, class ...
Kolja's user avatar
  • 233
6 votes
4 answers

Does internal state "leak" when it influences externally-visible behavior?

I have a method (in C++) which generates a value based on a parameter and the parameters from previous calls. Calling it more than once with the same parameter may generate different values each time. ...
sourcenouveau's user avatar
0 votes
1 answer

Is it a bad idea to use mutable for pre-allocated temporaries?

Let us assume I have the following C++ class: class MyFastMessageEncoder { public: MyFastMessageEncoder() : m_fieldEncoder(ENCODING_STYLE_DEFAULT) { } void set_encoding_style(...
Karl Nicoll's user avatar
0 votes
2 answers

Mutable cursor in readonly object?

I've been searching for the different uses of the keyword mutable. I've found that it is generally used for caching, lazy computing, mutex, ... But I'm wondering if it is coherent to use it for a ...
Grizix's user avatar
  • 9
21 votes
4 answers

What are the problems of bringing C++-like const into a language?

I am interested in the idea of C++-like const not that particular execution (like casting away const). Take for example C# -- it lacks C++-like const, and the reason for it is the the usual -- people ...
greenoldman's user avatar
  • 1,526
5 votes
1 answer

Are there advantages to declaring stack variables constant in C++

It's not clear to me what benefits there are of declaring your stack variables as constant in C++, I was hoping somebody might explain the benefits and purpose for this technique. For example: void ...
cppguy's user avatar
  • 161
4 votes
1 answer

Which is better to use? (Enum or Constant)

I have my enum like this. enum Size{PAGE_SIZE=20, HEADER_SIZE=30 }; I only use them as constants(No enum variable created) in my program. i.e= int x = PAGE_SIZE + 20; So what is the ...
sk patra's user avatar
  • 457
105 votes
3 answers

When and for what purposes should the const keyword be used in C for variables?

While getting my code reviewed here the issue of using the const keyword came up. I understand that it is used for implementing read-only behaviour on variables. I am confused about what are the ...
Aseem Bansal's user avatar
  • 3,004
1 vote
1 answer

Implementation defined behaviour changing const type in c

The C standard states that if an attempt is made to change a const type,the result is implementation defined.This gives error on my system,but what does it depend on(compiler,os)? What are the ...
user1369975's user avatar
  • 1,299
4 votes
2 answers

In C# what is lifetime or lifespan of constant variable?

In C# if i declare a constant variable is any memory allocated to it as it acts as a compile time replacement? How long is the variable's life?
baban's user avatar
  • 41
3 votes
4 answers

Immutable Method in Java

In Java, there is the final keyword in lieu of the const keyword in C and C++. In the latter languages there are mutable and immutable methods such as stated in the answer by Johannes Schaub - litb ...
Chris Okyen's user avatar
2 votes
1 answer

Reading data from file and const

Is it sane to let a read method on a file object to be const? For example size_t read(void* buffer,size_t length) const; The read method does not change the contents of the file, but updates the ...
user877329's user avatar
17 votes
7 answers

Zero as a constant?

I have come across this programming idiom recently: const float Zero = 0.0; which is then used in comparisons: if (x > Zero) {..} Can anyone explain if this is really any more efficient or ...
user avatar
13 votes
3 answers

Do people use const a lot when programming in Objective C?

Related: “sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers” warning Sometimes, I think it's useful though. I may need to pass an a table and want to make sure that the ...
user4951's user avatar
  • 709
40 votes
8 answers

Difference between immutable and const

I've often seen the terms immutable and const used interchangeably. However, from my (little) experience, the two differ a lot in the 'contract' they make in code: Immutable makes the contract that ...
K.Steff's user avatar
  • 4,505