Questions tagged [const]
The const tag has no summary.
38 questions
4
votes
4
answers
348
views
Is it reasonable to ignore const functions in Rust and treat them as a premature optimisation?
While I'm far from an expert at Rust, I'm becoming a proficient Rust user; I've written a substantial Rust library that is now being used in production. While I understand what const fns are, I haven'...
4
votes
4
answers
323
views
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 ...
0
votes
0
answers
169
views
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 ...
3
votes
1
answer
2k
views
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 { /...
1
vote
1
answer
274
views
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 ...
81
votes
9
answers
14k
views
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 ...
0
votes
1
answer
5k
views
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 ...
28
votes
7
answers
11k
views
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 ...
2
votes
2
answers
162
views
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 ...
0
votes
1
answer
121
views
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 ...
-6
votes
6
answers
382
views
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 ...
0
votes
2
answers
521
views
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 ...
12
votes
5
answers
6k
views
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 ...
26
votes
5
answers
22k
views
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. ...
23
votes
6
answers
11k
views
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){
}
often ...