310 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
Best practices
1
vote
5
replies
173
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.
...
3
votes
1
answer
200
views
C++ format string compile-time validity check
I am trying to implement a C++ Translate function for localization.
// Language package, containing key-value pairs of translation, e.g.,
// g_LanguagePack["HELLO@1"] = "Hello, {}!"...
2
votes
2
answers
187
views
C++20 Constexpr vector
The latest C++20 standard supports dynamic allocation memory in compile time with a limitation. The memory allocation cannot "go outside" the constant expression. That means that vector cannot be ...
2
votes
1
answer
173
views
Overcoming "division operator may only be applied to scalar values"
Application startup code looks like this:
CPU 8086
_start:
cld
mov ax, ds
dec ax
mov es, ax
mov ax, [es:3]
cmp ax, word (_endbss + 768) / 16
jae .mem
; Code would be ...
0
votes
1
answer
66
views
How does C++ handle constexpr evaluation for non-static member function pointers on runtime objects?
The code compiles and runs, but I'm trying to understand how the expression (obj.*funcPtr)(12) can be evaluated at compile-time when obj is not declared as constexpr. I would expect that this might ...
1
vote
2
answers
100
views
What is the best way to initalize 2D std::array along with its rows and cols?
I want to find the best possible way of initializing std:array in the below configuration (a better way would be able to use the cols and rows variables while declaring the array size):
class Engine {
...
-1
votes
1
answer
244
views
How to strip a string given as a const char[] through a constexpr function? [duplicate]
I want to define a constexpr function strip_string which allows:
Trim the string (removal of contiguous whitespaces in the beginning and end of the string.
Normalize contiguous whitespaces in the ...
0
votes
1
answer
603
views
What new types and operations can take place in a constexpr in C23?
C23 added a keyword constexpr which can be used to define objects as compile-time constants instead of enumerations or macros. For example:
constexpr int x = 10;
constexpr int y = x+10; // Valid
...
2
votes
4
answers
126
views
How to get the number of items in a Perl list constant?
The documentation states that a list constant in scalar context currently returns the number of values, but this may change in the future. It also states that, to use a list constant as an array, ...
3
votes
0
answers
101
views
Why doesn't this 'increment then decrement' generic const expression work in Rust?
I've been trying to make some of my code more airtight by moving some runtime operations to compile time. I thought it would be simpler because I'm only trying to increment or decrement a number by ...
-2
votes
1
answer
167
views
Require derived classes to provide a static const value
I have a base class and derived classes. The base class uses pure virtual methods to require the derived classes to implement methods - otherwise it doesn't compile. Classic.
Can I also require from ...
2
votes
1
answer
151
views
MSVC accepts constant pointer to int initialized with nullptr as constant expression
After reading about constant expressions I wrote the following example that compiles with msvc but is rejected by both gcc and clang.
int *const ptr2 = nullptr;
static_assert(!ptr2); //msvc passes ...
1
vote
1
answer
149
views
Can unexecuted branches of a conditional operator cause undefined behavior at compile-time?
As far as I can tell, the ternary ?: operator can be evaluated at compile time if at least the condition and the executed branch are compile-time constants. However, what if one of the unexecuted ...
4
votes
2
answers
195
views
How can I do short string optimization at compile time?
I have a basic string structure:
typedef struct String {
size_t size;
union {
char *ptr, buf[sizeof(char *)];
};
} String;
The idea is to be able to extract a char * from a String ...
1
vote
1
answer
81
views
Constexpr / Compile time constant expressions bugs
I get the following error in my project upon compiling. Tried everything but unable to resolve this.
Reproduced the error here:
https://replit.com/join/egfoiwpgig-darshanpandhi
Error
error: constexpr ...