999 questions
1
vote
2
answers
95
views
Transforming a UTF-8 string in a const context
I have a function capable of translating UTF-8 characters into a custom 8-bit encoding:
const fn char_to_custom_encoding(c: char) -> u8;
I'd like to apply it at compile time to a string literal so ...
0
votes
1
answer
125
views
How can this Nim function deal with the special case at compile time?
I'm not a Nim user, but I was astonished by how the solution for this exercise on Exercism.
The text of the exercise is as follows:
Your task is to convert a number into its corresponding raindrop ...
0
votes
2
answers
97
views
Is there a way to "configure" formatting at compile time? [duplicate]
I have struct/class that formats numbers. I want to tune its formatting parameters how i like. And this means I "need"(could be not, if there's another much more clever way of doing it?):
&...
3
votes
1
answer
99
views
What does "single run-time representation" mean in Bjarne Stroustrup's description of type erasure in "The C++ Programming Language"?
In The C++ Programming Language - 4th edition, in §25.3 at page 731-732 Bjarne Stroustrup shows a possible implementation of a Vector template class that's been specialized for all T* template ...
3
votes
1
answer
253
views
How can I iterate over struct fields at compile time in Zig to generate a serializer?
I'm learning Zig and I'm trying to write a generic function that automatically serializes any struct into JSON. In languages like Rust or Go, there are libraries that can iterate over the fields of a ...
2
votes
3
answers
232
views
C++: How to iterate over tuple in compile-time?
How to iterate over tuple in compile-time?
problem code:
#include <array>
#include <cstddef>
#include <tuple>
namespace {
class Solution {
public:
template <size_t ...
0
votes
1
answer
89
views
Compile‑time conversion of a JSON literal into a JsonString<char…> type in C++20 – overall design patterns? [closed]
I’m exploring ways in C++20 to represent a JSON text literal entirely in the type system, so that I can then write a metafunction like
template<typename Json, char... Key> struct HasKey;
...
0
votes
2
answers
92
views
DB2 LUW - Get Compile-time Failing Line Number in Stored Procedure
I've compiled other stored procedures (from SQuirrel© v4.8) and was able to figure out the error messages sufficiently to find my errors until I get a successful compile. But this time I'm getting ...
0
votes
0
answers
65
views
Can a function accepting Data.Vector.Fixed.Vector return Data.Vector.Fixed.Boxed.Vec2 or Vec3 based on the length of the input?
This is a bit of a follow up to a previous question.
Here's two functions that take 2 or 3 inputs respectively, and produce each a list of fixed-size vectors, of sizes 2 and 3 respectively:
import ...
0
votes
2
answers
188
views
Can I create a new string at compile time and use at runtime?
I am writing a log function that takes a format string and a variable number of arguments and captures std::source_location. This is not straightforward, so I tried the FormatWithLocation solution ...
0
votes
1
answer
127
views
Need help to understand how std::format can check its format string at compile-time
Out of curiosity, and with hope of learning useful compile-time techniques, I'm trying to understand how std::format performs compile-time checking of its arguments.
I started from the implementation ...
4
votes
4
answers
276
views
Check at compile time that a function call triggers a `static_assert(false)`
I have a template function with several type parameters and depending on those types, the function "calls" an static_assert(false). I want to know when my code fails to compile for specific ...
2
votes
1
answer
119
views
Filtering macro/array data at compile time in C
Is there a way to filter macro or array data at compile time in C?
For example:
#define THRESHOLD 5
#define MACRO_DATA \
X( 1 ) \
X( 8 ) \
X( 3 ) \
X( 12 ) \
X( 5 )
// ...
4
votes
1
answer
146
views
product enum class in c++
Is there a way to automatically (and at compile time) define a "product enum class" (i.e. an enum class that takes on values in the product space between two given enum classes)?
So if I ...
0
votes
4
answers
253
views
Compile Time Map Structure
I have commands list based on string and have function which needs to be called regarding per command. I'm thinking to use unordered_map to keep commands and functions.
std::unordered_map<std::...
1
vote
1
answer
165
views
how to profile compile time
Over time my project compile time has increased. But I don't know where the longest compile time is.
It's just a small application with only 2 translation units. One compiles to an exe and takes care ...
3
votes
2
answers
125
views
Compile time check if string contains quotes
I want to assert that an object JsonKey if constructed with a string that is known at compile time doesn't contain a quote. Here's what I have:
LiveDemo
#include<iostream>
template <size_t N&...
5
votes
1
answer
147
views
Why isn't constinit thread_local std::string possible?
The following code is not possible to compile. I'm wondering why:
#include <string>
constinit std::string constinit_string; // WORKS
constinit thread_local int my_int; // WORKS
constinit ...
0
votes
1
answer
151
views
Generate a compile time array of data using "include_bytes!" [duplicate]
I'm trying to load a folder of animation frames at compile time. The frames are named like this: frame1.png, frame2.png, ... frameN.png. Unfortunately after trying multiple methods for a while, I have ...
3
votes
1
answer
140
views
Is there a way to prevent a header file without header guards from being included at all at compile time?
I am trying to ban the inclusion of a list of header files from a source file, and thus preventing the dependency on classes defined in those headers.
There are tricks of checking the header guard ...
3
votes
1
answer
84
views
How to deduce the two dimensions of a compile-time matrix class with an initializer_list constructor?
#include <iostream>
#include <array>
template <typename T, size_t Rows, size_t Cols>
class Matrix {
protected:
std::array<std::array<T, Cols>, Rows> matrix;
...
2
votes
2
answers
185
views
populating an array at compile time in C
I have some embedded C code where I want to have a list of sine values. I have done something similar in C++ using a constexpr function that return an std::array outside of the main loop:
#include <...
0
votes
1
answer
139
views
Compile Time unique hash for variables
is there any ways of making unique hashes for each variable in compile time.
C++ allows block scopes which can result in same named variables inside one function so i cannot just do:
#define MACRO(...)...
2
votes
1
answer
153
views
How to reinterpret a value as bytes in a compile-time context in C++
I'm trying to implement a hash function that can be used at compile-time. To achieve this, I need to reinterpret a value (e.g., an int or a float) as its raw bytes.
However, when using compile-time ...
1
vote
2
answers
117
views
Container of std::variant of callable objects with bound parameters w/o using dynamic memory
I want to store several different callable objects (different types) in a container (e.g. std::array) in order to invoke them later.
Each callable has the same return type and the same amount and type ...
2
votes
1
answer
105
views
In GCC, is there a way to programmatically query which section a variable will be stored in?
I'm working with some code that targets an unusual embedded system that requires distinct processor instructions to read from its read-only memory.
For variables to be placed in said read-only memory, ...
1
vote
1
answer
274
views
const Iteration over values of an array
In Rust, it is possible to iterate over values of an array:
const COOL_NUMBERS: [i32; 4] = [1, 3, 3, 7];
fn do_something_cool() {
for number in COOL_NUMBERS {
// ...
}
}
But it is ...
0
votes
1
answer
299
views
Creating constexpr map filled by calls to a constexpr function in C++ 17
I have a constexpr function that calculates CRC at compile time. I need to create a map between CRCs that are generated by this function and string inputs and use them in runtime (Even better if it is ...
1
vote
0
answers
123
views
Concatenating two const arrays at compile time
I want to generate one const array of elements of type struct config; and enumeration enum pin;, which shall be used to index said array, from (configuration) definitions contained in two separate ...
3
votes
2
answers
120
views
Building a std::variant of pointers of types in the alternatives of another std::variant
I'm given a std::variant of several types. I would like to write a trait that gives another std::variant of the pointer of the types of the first.
using MyVariant = std::variant<int, float>;
...
0
votes
1
answer
151
views
difference between runtime and compile time with regard to dynamic binding [duplicate]
I am learning C++ and if I have classes
class Core {...}
class Grad: public Core {...}
then
Core* p;
Core& ref;
may be Grad objects which is not determined at compile time but at runtime hence ...
1
vote
1
answer
155
views
Pass compile-time type parameter to implementation at runtime?
I'm trying to write an API that validates the runtime type of typescript variables. I'd like to record what type of variable the user is expecting at compile-time and validate it at runtime; ...
0
votes
1
answer
257
views
Implementation of Static Flat Map
How to implement a flat map using arrays when keys are known at compile time. Imagine I know my keys:
enum class Asset
{
FONT,
TEXTURE
};
struct AssetContainer
{
};
Hence I could do:
struct ...
1
vote
2
answers
89
views
Mapping elements of a structure to different vector elements using reflection (compile-time)
I recently have hit a wall, and can't seem to find an elegant solution to my problem:
I have different data structures of different number of elements, all of which are aligned to pointer-size and are ...
2
votes
2
answers
143
views
C++ Compile-Time Class With std::vector Member Variable and Use It's Data at Run-Time [duplicate]
I am trying to make a writer class, which would be used to turn data into a binary stream, at compile-time using a std::vector, and then converting it to std::array to be used at runtime. eg:
#include ...
0
votes
1
answer
142
views
Accessing comptime array in runtime with for loop
I need to be able to loop through a comptime array in runtime, but since the indexes of the for-loop is calculated at runtime, I can't do that. Is there a way to do this?
const std = @import("std&...
0
votes
1
answer
187
views
How can I handle a string generated from macro within procedural macro?
I use include_str!() to import the string from a file and pass it into a proc_marco, but it does not work. I get an expected string literal error. Here is my code:
macro_rules! ptcl_layer {
() =&...
0
votes
2
answers
112
views
Is CallerMemberNameAttribute guaranteed to be evaluated before inlining?
Here is the documentation for the attribute. It does not mention when it is evaluated.
Here is the documentation for the greater 'CallerInformation' mechanism. It makes the following assertion:
...
3
votes
1
answer
94
views
Is there a way to constraint a type parameter to accept only types that are specific (rejects abstract type parameters)?
The following code compiles with no warning but does not work as intended.
inline def isXAssignableToY[X <: Matchable, Y]: Boolean = {
inline erasedValue[X] match {
case _: Y => true
...
3
votes
1
answer
269
views
How to validate strings to compile time?
I am trying to make the following code work:
#include <string>
#include <tuple>
#include <print>
template<char Specifier>
struct ParseSpecifier;
template<>
struct ...
1
vote
2
answers
92
views
C++ Template Arguments Sort
I'm looking for template code to sort template arguments by the return value of their static member function static constexpr int getType(), something like:
#include <iostream>
#include <...
1
vote
2
answers
111
views
Sums over overlapping rectangular slices of a 2D array
I need to calculate multiple sums, each sum being over an axis oriented rectangular slice of a 2D array. Many of the slices will overlap, and therefore there will be many shared terms in the sums. To ...
0
votes
2
answers
134
views
Function overloading depending on compile-time object state
Consider having a simple class, that writes integers to cout depending on its state:
#include <iostream>
class NotAWriter{
public:
NotAWriter& operator<<(const int& arg) {
...
2
votes
1
answer
70
views
Is it possible to emit a dynamically generated compiler error message in inline methods or clauses?
Summoning an instance by means of a method context parameter allows to customize the error message declaratively annotating the parameter with @implicitNotFound which requires a literal string that ...
3
votes
3
answers
283
views
How to check if a macro argument is an integer literal in C
I'm trying to do a macro like this:
#define STRING(i) \
struct STRING##i \
{ \
size_t len; \
char chars[i]; \
}
but the problem is this works with constexpr arguments like this:
constexpr int ...
0
votes
1
answer
46
views
Why can I not get a compile-time error referencing a non-existent key in a Dart map?
I am new to Dart and was expecting to get a compile-time error for the following code:
final Map screens = const {
"START": 'start_screen',
"QUESTIONS": 'questions_screen'...
0
votes
1
answer
92
views
Placing data at an address given by a constant arithmetic expression [duplicate]
Context
My team upgraded our project from using Arm Compiler for Embedded 5 to Embedded 6. In our code, we place data at a specific location in RAM like so:
#define RAM_START_ADDR <some constant>...
0
votes
2
answers
115
views
Is there an effective way to assert an constexpr-if branch is executed?
int f(auto obj) {
if constexpr (HasFastGetData<decltype(obj)>) {
return obj.FastGetData();
} else {
return obj.GetData();
}
}
int main() {
B obj;
f(obj);
...
9
votes
2
answers
346
views
What is the storage duration and lifetime of a non type template parameter and how can it be used for compile-time computation?
In C++ Weekly - Ep 313 - The constexpr Problem That Took Me 5 Years To Fix!, Jason Turner demonstrates several compile-time techniques in order to construct a std::string at compile-time and then pass ...
0
votes
0
answers
35
views
How to Create constexpr Array Where Elements Depend on the Array’s Size? [duplicate]
How can I create a constexpr array, where the array elements are calculated and depend on the size of the array? Look at the example below in the initializer list to see what I mean.
template <...