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.

Filter by
Sorted by
Tagged with
1 vote
1 answer
95 views

c++ returning const reverse iterator from a class method, but method cannot be const?

I am trying to return a const reverse iterator from a class method. But my code doesn't compile when I mark my method as const. Without const the code compiles with no troubles. Any idea why? #include ...
Miroslav Krajcir's user avatar
-1 votes
0 answers
56 views

Is there any way to remove the need for const_cast in this case?

I am writing a data structure for a prefix iterator template class to perform some DFS. Iterator& operator++() { TTreeNode lPopped = const_cast<TTreeNode>(fStack.top()); ...
Simplylmk's user avatar
  • 265
-5 votes
0 answers
41 views

Constants of Java in OOP [closed]

How to implement a constructor that takes a double parameter and initializes a final double constant named GRAVITY with the provided value? To learn about constants. Because constants are a valuable ...
Sasith Avinda Abeysundara's user avatar
9 votes
4 answers
869 views

Does a const char* literal string persistently exist as long as the process alive? [duplicate]

I have functions like the following: const char* get_message() { return "This is a constant message, will NOT change forever!"; }; const char* get_message2() { return "message2&...
Leon's user avatar
  • 1,651
1 vote
0 answers
55 views

Need more understanding of PHP Float CONSTANTS

So, I just came across these PHP float Constants: PHP_FLOAT_DIG and PHP_FLOAT_EPSILON, and I don't understand both constants (even after studying the official documentation https://www.php.net/manual/...
divad's user avatar
  • 117
0 votes
0 answers
28 views

Rust refs vs Cpp refs. Clarification around references, assignments, and copying in Cpp vs Rust [duplicate]

I'm coming back to cpp after 15+ years and suffering confusion regarding the many ref semantics. I haven't worked with c14 (onwards), and am trying to reason about things. I've been doing Rust ...
Avba's user avatar
  • 14.9k
1 vote
1 answer
34 views

Linked List Assignment in C: assignment discards ‘const’ qualifier from pointer target type

I was assigned a doubly linked list assignment in C and am just about done, but I can't figure out one warning that I am consistently getting. The warning is "assignment discards ‘const’ ...
Derek Kwon's user avatar
-3 votes
0 answers
93 views

Understanding PHP Float Constants [duplicate]

So, I just came across these PHP float Constants: PHP_FLOAT_DIG and PHP_FLOAT_EBSILON, and I don't understand both constants (even after studying the official documentation https://www.php.net/manual/...
divad's user avatar
  • 117
0 votes
1 answer
74 views

Does golang have int32 overflow? [duplicate]

func overFlowTest() { maxInt32 := math.MaxInt32 overflowedValue := maxInt32 + 1 fmt.Println("Max Int32:", maxInt32) fmt.Println("Overflowed Value:", ...
mercuryRising's user avatar
1 vote
3 answers
179 views

How to "merge" 2 functions (const & non-const i/o) into one?

Is there a syntax for merging the following 2 functions into one, so that if input is const, result is const, and if input is non-const, result is non-const? char* f1(char* x) { return x+1; } ...
Sasha's user avatar
  • 339
0 votes
0 answers
39 views

Compiling in GCC but not in Clang: "initializer element is not a compile-time constant"

I'm having this error (in the title) when I use the compiler clang-15 (15.0.7). Below is a minimum reproducible code: #include <stdio.h> #include <stdint.h> #define BUF_SIZE 255 #...
rammmmm_'s user avatar
1 vote
2 answers
104 views

const void * vs. void const * [duplicate]

I am getting different answers for this question. I know that the order for other data types doesn't matter/simply by preference (e.g. const char * and char const *). Not sure if the same applies to ...
sarias's user avatar
  • 11
0 votes
2 answers
52 views

Avoiding GCC compiler "incompatible pointer type" warnings

I've written two functions. One for copying an array of char type: void char_arr_copy(const char *src, char *dst, int arr_size) { int i; for (i=arr_size-1; i >= 0; src++, dst++, i--) ...
Fyodor's user avatar
  • 29
0 votes
0 answers
32 views

knapsack optimization using Python pulp - how to set constraints?

I want to develop inventory fulfillment optimization model. I want to fulfil from Plt_Inv to WH_Inv if WH_Inv is less than SS for each SKUs, with total fulfillment volume should be less than 10000KG. ...
Sattai's user avatar
  • 1
0 votes
1 answer
131 views

Constant Expression Expected Error when Initializing Array of Records

I am facing an issue while trying to initialize a constant array of records in Delphi. Here's a simplified version of my code: TParameterList = record Parm_Name: string; Parm_Unit: string; ...
Issam's user avatar
  • 67
1 vote
0 answers
67 views

Why can I add "const" to value parameters but not to reference parameters on member definition? [duplicate]

Look at this code: struct A{ void f(int); void g(int&); }; // this works, defines A::f(int); void A::f(const int){// no compiler error here despite additional const }; // why does this ...
phinz's user avatar
  • 1,397
0 votes
1 answer
44 views

Javascript const list combining multiple keys

I am working on node js project where I am defining const list with multiple keys having same values. Can we combine it like we do it in switch..case statements if multiple cases are having same ...
user734172's user avatar
0 votes
1 answer
77 views

How to initialize a const primitive from a function returning a tuple with explicit type [duplicate]

I have a function that returns const values not by reference as in this question. I would like to call the function and initialize the const variables when I call the function but it isn't possible. I ...
heretoinfinity's user avatar
0 votes
0 answers
68 views

Understanding const Qualifier Position in Template Parameter Deduction for Pointers

I'm working on understanding how the const qualifier affects deduced types in C++ template functions, especially when pointers are involved. Consider the following template function: #include <...
Sami's user avatar
  • 586
2 votes
0 answers
42 views

Reactjs disruption in "getStorageTheme()"

In my project, there is a shape image. When I click the dark mode button, it turns the color I want. When I click the light mode button, it also turns the color I want. However, when I refresh the ...
Berkant Karakayış's user avatar
0 votes
1 answer
44 views

When exactly is the space allocated in memory for a constant in Java at compile-time or runtime?

I'm learning about Java constants and I want to know when exactly is the space allocated in memory for a constant. Is it at compile time or runtime? For ex I have a constant like this: public static ...
elvis's user avatar
  • 832
-2 votes
1 answer
53 views

Can someone explain why is Int(total) difrent value than Double(total)

I don’t understand why Double(total) value is 15600 and Int(total) value is 15599? func race(_ v1: Int, _ v2: Int, _ g: Int) -> [Int]? { var vd: Double = Double(v2) - Double(v1) var total = ...
Stefan Stojkovic's user avatar
0 votes
0 answers
35 views

Compile-time constants when using object reference in Java

I'm learning about compile-time constants and I want to ask you if that instance.maximumLoginAttempts expression is a compile-time constant. I don't think it is because in my opinion instance ...
elvis's user avatar
  • 832
0 votes
0 answers
15 views

Using onclick with switch case procedure and Gradle 8.0, why can I not use the id from the button

I do not use audios in apps often, a few years ago I did and used the following to play audios as required by the user. The R.id.**** is the id of the button. ''' public void onClick(View v) { ...
ChinaTonka's user avatar
3 votes
1 answer
102 views

Force GCC to always load a const (i.e. read-only) even with optimizations enabled

Let's say I have a const global variable that is, therefore, read-only and will be placed in the .rodata section. I'm compiling the code with -O3 optimizations. This will make the compiler (i.e., GCC) ...
josecm's user avatar
  • 465
0 votes
1 answer
134 views

const reference to a pointer

When I have a const reference to a pointer, how come I am able to change the value of the object the pointer points to, does the const means in this example, address stored in pointer cannot be ...
semicolon_missing's user avatar
0 votes
2 answers
87 views

How to initialize static const members of a class which has the same type as the class? [duplicate]

I have a class whose static const members have the same type as the class. I am getting an error that class isn't yet complete. #include<iostream> class Color { public: Color(unsigned char ...
Praveen O'Connell's user avatar
0 votes
1 answer
73 views

From which php version the constant PHP_INT_MAX exists?

Is this documented somewhere from which version any specific PHP constant is defined? Specifically, I would like to know about the following two: PHP_INT_MAX PHP_INT_MIN Someone has mentioned here - ...
HarshM's user avatar
  • 39
0 votes
1 answer
53 views

Clarification about conv.qual#1

conv.qual#1: A qualification-decomposition of a type T is a sequence of cvi and Pi such that T is “cv0 P0 cv1 P1 ⋯ cv_n-1 Pn-1 cvn U” for n ≥ 0, where each cvi is a set of cv-qualifiers ([basic.type....
Enlico's user avatar
  • 23.9k
0 votes
2 answers
75 views

How to implement const-object-specific constructors in C++?

As an exercise I implemented a class called Array to simulate most of the functionalities of the class std::array. One thing I struggle to achieve is the following. Let's say we have the following ...
bhuseinovi1's user avatar
-1 votes
1 answer
107 views

Compile time endian dependent expressions?

While implementing UDP BitTorrent tracker protocol, I noticed that most common packet parsing step is checking whether first 64 bits are protocol id 0x41727101980, used for requesting a connection, ...
user avatar
0 votes
1 answer
49 views

C: void* as handle, loses const expressiveness

I try to consider an API strategy where, in the header file, instead of typedef struct {...} type_t; extern void func(const type_t* instance); I have typedef void* type_t_handle; extern void func(...
Sasha's user avatar
  • 339
0 votes
1 answer
22 views

In JMeter,when I add constant timer(5000ms) average time increases. I expected by adding a constant timer response time would reduce and become faster

In jmeter, i am observing an increase in response time when i add a constant timer in JMeter, and i am expected that adding a timer (sleep) would reduce the response time and make it faster. I added ...
mayuri's user avatar
  • 167
0 votes
4 answers
101 views

Avoiding GCC compiler warning "return discards ‘const’ qualifier from pointer target type"

I have the following function: item *already_met_the_number(const item *p, long n) { while (p) { if (p->num == n) return p; else p = p->next; } ...
Fyodor's user avatar
  • 29
0 votes
0 answers
41 views

Two const variables with same name in 2 independent .cpp file will lead to a link error?

Thanks for reading my question. I understand that by default const objects are local to a file and the key word extern would be the best practice to share a variable across different files. Chatgpt ...
Yiqun Cui's user avatar
0 votes
1 answer
42 views

Microsoft Word Subtract constant from date's year

How can I subtract a constant from the current date's year in Ms-Word? Attempt { = {DATE\@"YYYY"} – 1983 \#"00" } I keep getting: !Syntax Error, {
Mihai Socaciu's user avatar
1 vote
0 answers
49 views

How to wrapping around STL containers and get proper const and non-const behavior?

When wrapping a container type e.g. std::vector< T > where T is a smart-pointer type e.g. std::shared_ptr< SomeType >, is there a way to simultaneously ensure const correctness and enable ...
joaocandre's user avatar
  • 1,641
0 votes
0 answers
9 views

Use Constants inside of Doctrine Attributes for types

I have Entity and have some fields related to DateTime, for example one of them #[ORM\Column(name: 'enter_date', type: 'date', nullable: true)] #[Type('DateTime<"Y-m-d">')] ...
yAnTar's user avatar
  • 4,319
2 votes
1 answer
98 views

How to perform complex initialization of a const data member in the member initializer list

Is there a way in modern C++ to initialize a const value in a class derived from accessing a pointer argument? and that pointer might be null, so basically after doing a check on the pointer? e.g. // ...
Raffaello's user avatar
  • 1,675
0 votes
1 answer
29 views

Where does the aio_threads defined in glibc?

// file: glibc/rt/aio_misc.c /* User optimization. */ void __aio_init (const struct aioinit *init) { /* Get the mutex. */ __pthread_mutex_lock (&__aio_requests_mutex); /* Only allow ...
hello_god's user avatar
  • 173
-1 votes
1 answer
64 views

C# object's returning internal components

Goal A very recurrent design I would like to implement in C# is following: A class, that owns multiple instances of another class. For clarity let's pick an example, let's say a "car" that ...
mgueydan's user avatar
  • 391
0 votes
2 answers
82 views

const and destruction responsibility in C

Suppose I have a first struct type a_t and a second struct type b_t, and one of the fields of b_t is a pointer to an instance of a_t. I have creation and destruction functions: b_t* b_t_create(a_t* ...
Sasha's user avatar
  • 339
-2 votes
1 answer
59 views

binding reference of type 'type' to value of type 'const type' drops 'const' qualifier

This issue looks like known one. but i have no any const in types. What I am missing? I have the following code: class PlanForBlock { public: void registerExtraIterator(DummyLoadIterator *...
YAKOVM's user avatar
  • 9,911
-4 votes
1 answer
53 views

the object has type qualifiers that are not compatible with the member function "Customer::displayCustomer"

I'm currently working on a project for an online banking service and I keep running into the same problem here no matter what I try. The line in question I keep getting an error for is commented in ...
gbolive011's user avatar
4 votes
3 answers
117 views

Is it safe to change data via a pointer when another pointer-to-const observes it?

Is it a safe code, or can the compiler optimize access through p, such that *p will result in 42? #include <stdio.h> int i = 42; int *d = &i; void test(int const *p) { *d = 43; ...
Hren Sgory's user avatar
1 vote
1 answer
48 views

a function pointer as a function paramter - should one const it? [duplicate]

If I write something as follows: int f(char x, const int (*g) (const char x)) { return g(x); } does the first const say effectively what I think it says, namely basically that the programmer can'...
Sasha's user avatar
  • 339
0 votes
1 answer
36 views

Is it possible to write a raw pointer wrapper with support for "array_conversion" when adding const?

Consider the following minimal class (Godbolt link) as a motivating example for my question: class A { public: explicit A(std::vector<int *> elements): _elements(elements) {} auto ...
Henk's user avatar
  • 836
1 vote
3 answers
79 views

postgres SQL - use upsert to swap values in 2 rows

I have the following table with a combo key columns CREATE TABLE IF NOT EXISTS MyTable ( ColumnID1 integer NOT NULL, ColumnID2 integer NOT NULL, ColumnName character varying(1000), ...
unexpectedly's user avatar
0 votes
1 answer
16 views

How to add link/href to html from global constants file

I have a global constants file where I added a links node to it but I'm not sure how to translate that node from the constants file into the html file or possibly ts file of the html file. constants ...
developer8492's user avatar
0 votes
2 answers
47 views

Javascript compiler error in Chrome's console

I was doing some javascript coding in chrome's console. When I do: console.log(x); let x; It gives "ReferenceError: x is not defined" But when I run the below code: let x; console.log(x); ...
Akrit's user avatar
  • 99

1
2 3 4 5
207