10,427 questions
6
votes
2
answers
276
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 ...
3
votes
3
answers
199
views
Is a function call returning a pointer a prvalue?
This question arouse from the question "why is const in function return type, which is a const pointer, ignored?" (const pointer, not pointer to const)
int* const Foo();
is identical to
int*...
8
votes
2
answers
271
views
Why should I be careful in "noexcept-ing all the functions" just like I "const all the things"?
I haven't found a video about const all the things ¹, but there's at least Con.3: By default, pass pointers and references to consts from the Cpp Core Guidelines.
There's lots of presentations and ...
2
votes
2
answers
179
views
How does interpolating a Perl constant into a string work?
I've just come across such usage of Perl constants:
use strict;
use warnings;
use constant PI => 4 * atan2(1, 1);
print("The value of PI is: ${\PI}\n"); # The value of PI is: 3....
5
votes
0
answers
144
views
How do I get CLion to prefer T const& over const T&?
CLion supports a "code inspection" named "pass value parameters by const&", which suggests you prefer passing by const& rather than by-value with a copy.
When you accept ...
1
vote
0
answers
29
views
Cannot index const object by known property in TypeScript [duplicate]
I have a complex but well-typed constant object such that no matter what the first and second level keys are, there is always a particular known key at the third level (in the example below, moons). ...
5
votes
5
answers
418
views
Why Zero has no decimal integer spelling in C?
I am reading modern C, and on page 66, I come across following section:
Remember that value 0 is important. It is so important that it has a lot of equivalent
spellings: 0, 0x0, and ’\0’ are all the ...
4
votes
2
answers
157
views
Is assigning string literal to mutable char* legal on newer language revisions or just warned against by compilers?
Related to Is it bad to declare a C-style string without const? If so, why? , but that question is about best practise, while mine is more language lawyer. Similar questions have been asked, but for C+...
3
votes
2
answers
93
views
Should I care about Wcast-qual errors?
I recently read the NASA's power of 10 rules, and thought the rule about "using the compiler's most pedantic setting" was very interesting, considering warnings exist for a reason. I already ...
2
votes
2
answers
126
views
Disparity in errors on passing const-argument value to a non-const parameter of a function in C++
I was trying to understand the use of 'const' keyword with pointers from the source: GeeksforGeeks.
It is mentioned that "Passing const argument value to a non-const parameter of a function isn't ...
1
vote
1
answer
157
views
C++ builtin constexpr vs CUDA __constant__ for higher dimension array
I recently need to implement a Sobol sequence generator by CUDA.
Now to pre-store the Sobol table, I can either use a C++ native constexpr like below:
constexpr unsigned int sobol_table[NUM_DIMENSIONS]...
1
vote
0
answers
68
views
Passing newly created const-qualified objects as array indexes (or slices) requires parentheses
I encountered a situation when a newly created const-qualified std::valarray<size_t> is passed to another std::valarray’s subscript operator [] when defining a std::indirect_array. This worked ...
6
votes
4
answers
208
views
Initialize constant string array out of order [closed]
Trying to increase robustness of a piece of embedded code - simplified like this:
enum tags {
TAG1,
TAG2,
NUM_TAGS
};
const char* const vals_ok[] = { "val1", "val2" };
...
9
votes
1
answer
260
views
Cannot match on Result in const fn due to "destructor cannot be evaluated at compile-time" even when value isn't dropped
I tried to match on a generic Result<&mut T, T> inside of a const fn, but the compiler did not let me. It would require being able to drop the value at the end of the scope, which is
...
0
votes
1
answer
96
views
Constant Declaration Conventions
So far I've been declaring my constants in the "Main" module for my workbook, below the "Option Explicit" but above the "Sub DoTheWork()."
Do public constants have to be ...
6
votes
2
answers
251
views
Design pattern for const structures?
Let's say I have to read some configuration files to set fields of a structure. As soon as the file has been read and the structure initialized, its field's values should not be changed.
I read many ...
1
vote
3
answers
232
views
Does declaring an unused const variable in C lead to undefined behavior?
I know that unused variables in C typically just lead to compiler warnings, and const variables are meant to be read-only.
However, I’m curious: according to the C standard, is there any scenario ...
1
vote
2
answers
93
views
Why is it allowed for a const pointer to be passed as non-const argument of a function and get modified?
I am able to pass a const pointer as argument of a function that takes non-const pointer.
Furthermore, inside the function body, I was allowed to modify its value as well with only a warning:
(passing ...
0
votes
1
answer
111
views
Pass parameters to exe-file (compiled from Simulink)
I have a simple Simulink model that passes a constant variable to the workspace.
I've converted the Simulink model to an exe-file using Simulink Coder (with grt.tlc as the target system file). ...
3
votes
2
answers
115
views
What is the const qualifier attached to in C: the memory area or the pointer?
I wondered, if the const qualifier in C is the attribute of the pointer or the memory area?
If I do something like:
struct S { int data; }
struct CS { const int data; }
char *p = malloc(100);
struct ...
2
votes
2
answers
119
views
Are string-backed enums useful as string constant dumps in PHP?
In order to get in touch with PHP, I began a little website project from scratch.
The project steadily grew so that now, I'm refactoring my codebase.
Up until now, I made use of constants which I use ...
1
vote
1
answer
114
views
Why can't I declare class-scope constants without using 'static'? [duplicate]
I'm an OOP newbie and just learning classes. Why can't I create constants and use them in classes without the static specifier? Like this:
class MyClass{
private:
const int MyConst = 10;
int ...
1
vote
0
answers
94
views
Rust ndarray, statically allocate an array
I have an ndarray array that is used throughout my program:
Note: ndarray and nalgebra are both used here, imported as nd and na respectively.
let test: nd::ArrayBase<nd::OwnedRepr<na::...
2
votes
3
answers
157
views
Lazy initialization of const attributes in c++
I would like to carry out a lazy initialization of a set of (std::vector) attributes in c++. They have to be const, in the sense that after the first time they are initialized (via a get method), ...
4
votes
5
answers
222
views
Declare array based on size of another array
I have an array of color codes, the size of which is known at compile time. I want to declare another array of the same size. But the code below throws an error.
I can, of course, declare the size ...