Questions tagged [constants]
Constants in programming are definitions whose value is fixed throughout a program's execution. Literals in most languages are constants, for example. In referentially transparent programming styles, all definitions are constant. A const-qualified data storage area (object, field, variable, parameter) is one that "never changes", thus allowing extra code generator optimizations and additional static checking of program correctness.
constants
10,364
questions
0
votes
2
answers
44
views
Different behavior when using const before a const constructor in Dart
In the Dart documentation, it states that we should not place const before initializing a const constructor, as the keyword is already implicit.
However, can someone explain why the following codes ...
1
vote
0
answers
46
views
How am i able to change constant class member? [duplicate]
I came up with the way to change members of constant class and i don't have an explanation for it.
I came up with this example of code:
#include <iostream>
class Cool_class
{
public:
int ...
0
votes
2
answers
62
views
c++ 'const' in multi-file
In C++ Primer, it tells me that I should modify all const variables with 'extern'. However, when I tried that in DevC++ 5.11 with C++11, I found that it was not necessary.
//main.cpp
#include <...
1
vote
1
answer
78
views
Exposing what the compiler does for passing to a reference vs to a const reference
Compiling the code shown below, and examining the disassembly (objdump -DSs a.out > a.dis), I am unable to see any difference in what happens for invoking fun_ref(T &) and fun_const_ref(const T ...
1
vote
1
answer
82
views
How to return an address of a const struct member in compliance to MISRA from a C function?
I have a C function that gets the address of a struct member and returns it as a void pointer.
The first parameter of the function provides a pointer to the struct. This parameter is provided as const ...
1
vote
2
answers
51
views
How to Expose Python Enum as "Constants" without the Class Name All At Once [duplicate]
The following code creates three "constants" that can be used without the enum's class name before each one. Is there a way to do this to all members of the enum without having an explicit ...
7
votes
1
answer
290
views
Is it well defined to cast to an identical layout with const members?
Consider the following type:
template<typename T> struct View
{
T* data;
size_t size;
};
Is it valid to cast from: View<T>& To View<const T>&?
Does it invoke ...
1
vote
1
answer
76
views
Char array has different address when copied by pointer
What I'm trying to do is cheat the const-ness of the member variables of the class. So I have the following code:
"StringView.hh":
#pragma once
#include <cstring>
class StringView {
...
4
votes
1
answer
71
views
typedef for constant pointer to constant data function array
I have a C header file (.h):
typedef uint8_t paraFunction(uint8_t paraVal, uint8_t paraNum);
paraFunction *paraCallTable[256];
And I have a C source file (.c):
paraFunction *paraCallTable[] = { fn1,...
0
votes
1
answer
41
views
Use enum as const type parameter
I know you cannot use an enum as a const type parameter in Rust currently. In particular this doesn't compile:
struct Foo<const F: Fruit = { Fruit::Apple }> {
...
}
Only integers, char and ...
6
votes
1
answer
94
views
Returning const reference from lambda stored as std::function segfaults [duplicate]
Here is the code:
#include <vector>
#include <functional>
class TestClass {
public:
const std::vector<int> &getStuff() const {
return callback();
}
protected:
...
2
votes
0
answers
53
views
Python | Material Science | Graphene Energy Simulation Issue: Unexpected 'K' Constant Factorization in Atomistic Structure Calculation
I'm currently working on an energy calculation for an atomistic structure (Graphene) and would appreciate some help in verifying my calculations. Specifically, I want to ensure that certain constants ...
0
votes
1
answer
99
views
Compilation error when using C++ template functions that accept as arguments another functions that accept references to pointers [duplicate]
Here is the most basic example of the C++ code that raises this compilation error:
template <typename T>
void TestF(int(*func)(const T&)) {
}
int IntNormal(const int& x) {
return x;...
1
vote
1
answer
134
views
How can I ensure constant evaluation when using generic consts?
Attempting to evaluate two generic constants into a new const is denied by the compiler. Here I'm trying to convert between generic variants of Foo:
struct Foo<const X: isize> {
n: isize,
}
...
0
votes
3
answers
159
views
when we put a "const" in front of a function definition?
I did not find any difference if I have a const in front of a function definition or not.
template<typename T>
class coor {
private:
T x;
T y;
public:
auto X()const { return x; }
...