Open jaskij opened 1 year ago
This came about because, while GCC will do what it always does, constexpr
in theory gives more avenues for optimization than a simple const
, especially if said const
is extern
.
To add: just now, the lack of constexpr
has prevented me from using static_assert
to make sure correct compile-time options were selected.
Edit: which was solved by including the generated .c
file, but that always feels... bad.
Hi @jaskij, you certainly aren't alone in writing firmware in C++. I've been involved in doing just that on-and-off for the past 20+ years. This is a great idea!
Regarding C++ standards, I'd be keen to keep it to features from C++14 or earlier. This will give the best chance of aligning with a validated compiler for heavily regulated industries such as medical device development and safety systems. Implementing the actual format itself will be pretty straight forward (I've already done the boilerplate :-) )
As for the question around inline vs static vs [other option], I've reached out to a couple of friends and will give it some thought. Others who contribute to SRecord may also want to offer advice.
Meanwhile, regarding:
Edit: which was solved by including the generated
.c
file, but that always feels... bad. Using the−INClude
option will generate a .h intended to be included. If I've understood your dilemma, this may alleviate your conscience.
Regarding C++ standards, I'd be keen to keep it to features from C++14 or earlier.
From my knowledge, the only interesting point is inline
, which was introduced in C++17. And which was #ifdef
ed in my proposal because of it. Hopefully I got the feature macro correctly.
Using the −INClude option will generate a .h intended to be included. If I've understood your dilemma, this may alleviate your conscience.
No, the C header doesn't work, because extern const
cannot be used in a constexpr
expression. And while I know that in practice static const
is the same, I still prefer getting the constexpr
. C++ is a bit too big to be able to accurately predict compiler behavior.
I work in a small company, with a lot of freedom, so I luckily have the option to use basically whatever ARM's latest release of GNU Toolchain supports (C++20, at the moment).
I got reminded of this issue recently, and just today learned about C23's #embed
. Reading further ( https://thephd.dev/finally-embed-in-c23 ), it should probably make it into C++26.
Call me crazy, but I program microcontrollers in C++.
To that end, while the C array is perfectly adequate, over the years the languages are diverging more and more. I propose adding a new output format, the C++ array.
The question is of specific output format, how it should be done.
My idea is to:
To that end, I believe the outline below to be appropriate. Although I would love for someone with a better grasp of the C++ standard to check if what I'm doing here with
inline
vsstatic
is the right way.More on the
inline
keyword: cppreference.com notes:If my understanding is correct,
inline
will result in more of a guarantee that the array will not be duplicated in the resulting program/library.