All Questions
10,433 questions
Score of 8
3 answers
322 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
282
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
196 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
1277 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
174 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
193 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
156 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
57
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
197
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
167 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
151 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 ...
Score of 6
2 answers
363 views
How do I follow "C.12 - Don’t make data members const or references in a copyable or movable type" while keeping track of some const value?
When I have some const members variables in my class, I would like to know how to respect core guideline C.12 to keep a default copy-assignment operator.
C.12: Don’t make data members const or ...