10,433 questions
Score of -1
1 answer
92 views
Why is vector<int> const not the same type as vector<const int> const?
We know that for "value-type" containers in C++, like std::vector, the container being const means that the elements can't be changed either, i.e. they are effectively const.
Why, then, is ...
Score of 8
3 answers
331 views
How to embed a const char in a const string?
Consider the following two lines of code:
const char c = 'a';
const string s = "" + c;
The 2nd line is giving me error CS0133: The expression being assigned to 's' must be constant.
...
Best practices
1
vote
10
replies
295
views
Inline and extern keyword for constants
I learned about inline variables in C++17, but I still have some confusion about them.
When should I use extern variables versus inline variables for sharing constant global variables across multiple ...
Score of -3
3 answers
198 views
"Modification of a read-only value attempted" when using JSON object
A few years ago I wrote some Perl code using JSON. It still worked in SLES15 SP6, but in SLES15 SP7 I get an error like this:
Modification of a read-only value attempted at (eval 73)[/usr/lib/perl5/...
Score of 11
2 answers
1281 views
Is type qualifier on return type meaningless?
If I have a function template returning const-qualified type (with a trivial body for example):
const auto f(auto) { return 1; }
and I want to get a pointer on a particular instance of the function
...
Score of 4
2 answers
176 views
Accessing constant variables from a lambda in Visual Studio 2026
I have a program that I would like to build in Visual Studio 2026, but struggle with the errors appearing.
One simplified piece of code is as follows:
void f(const void*) {}
int main() {
...
Score of 1
4 answers
206 views
Casting in comparator functions for qsort
I'm new to pointers and trying to wrap my head around how casting and dereferencing should function in comparator functions that qsort takes. My understanding is these always have the signature int ...
Score of 1
2 answers
195 views
C implicit const casting (in dereferencing order) should be permitted but isn't
For context, I have a function to print some text, which naturally takes in a const char *const * as one of its arguments (meaning the provided text will not be modified in any way by the function). ...
Best practices
1
vote
11
replies
6k
views
Should a C++ logger write method be defined as const?
I'm hesitating whether the Logger Write method should be marked as const:
class Logger {
public:
// Write log message to the terminal
void Write(const string& msg) const;
...
};
In this ...
Score of 14
2 answers
2299 views
Does a const qualifier inside a struct member declaration do anything?
struct foo {
const int bar;
};
struct foo {
const int *bar;
}
Do the 2 const qualifiers have any role?
Score of 0
1 answer
158 views
How to avoid "Not a HASH reference at ..." in regex like "qr/^${\CONSTANT}{3,}$/"
In a script I'm using constants (use constant ...) to allow re-use ion actual regular expressions, using the pattern from https://stackoverflow.com/a/69379743/6607497.
However when using a {...} ...
Best practices
0
votes
3
replies
58
views
If my SwiftUI View's sub-views based off constant Boolean values, can I fix them in advance?
Let's say my View has a bunch of sub-views, controlled by switches.
struct MyView: View {
var body: some View {
if A {
AView()
} else {
BView()
}
}
}
But A is a constant ...
Best practices
1
vote
5
replies
199
views
Encoding character data in a constant array of bytes
Defining a string and converting it to bytes at runtime is one thing, but I would want to have a constant array of bytes defined at design time and then pupulate it with a mix of bytes and strings.
...
Score of 4
2 answers
169 views
Error with creating an array of named/predefined string constants in c
For background, I am working to create a version of minesweeper that runs in the terminal to become more familiar with coding bigger projects in c.
I am coding in VScode, to run on a Linux server (...
Score of 2
1 answer
152 views
Accessing Union Members with Different Qualifiers in C
In the C standard (at least, I'm looking at draft C23 WG14/N3088), section 6.7.3 Type Qualifiers states:
7 If an attempt is made to modify an object defined with a const-qualified type through the ...