Comments (2)
Hi @ponast,
I try not to abuse usage of constexpr
in my work in general, and it CommsChampion Ecosystem in particular. Although C++ is quite flexible in allowing indiscriminate usage of this keyword and spits out errors only when the result of constexpr
function is attempted to be used for compile time code generation conditions (such as using classes from type_traits
), but evaluation of the return value at compile time cannot be calculated.
My convention is to add constexpr
only for functions that are expected to be used for compile time evaluation, mostly for boolean expressions of std::conditional<...>
or similar. It becomes a "contract" for the function that it's usage of the compile time evaluation will never fail. Once constexpr
- always constexpr
. Anything else I leave for the regular compiler optimizations.
As for the suggested change, the constexpr
on the return type of const char*
doesn't make much sense, it doesn't preserve the size of the string, i.e. it's not const char[N]
. To allow the compile time strings the name should be a static variable, rather than a function
static constexpr const char NameStr[] = "some_name";
However, my experiments show that some compilers (clang for example) don't handle this case properly. The link stage reports error of inability to find the string symbol:
/usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x23
...:(.text+0x14e2e): undefined reference to `SomeField::NameStr'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
However if the constexpr string is put in the anonymous namespace in the cpp file instead of the static member of the class, everything compiles normally.
Also worth mentioning that when developing initial versions of the code generator I intended to make sure that the strings are not duplicated, i.e. having single class accumulating all the strings to avoid repetition. However, when analyzing the binary code produced by the compiler I noticed that it cleans up all the duplicates by itself during its optimization stage, i.e. the following classes will reference the same and the only "SomeName" string in the read only section of the binary.
struct SomeField
{
static const char* name()
{
return "SomeName";
}
};
struct SomeOtherField
{
static const char* name()
{
return "SomeName";
}
};
As the result complicating the code for strings storage wasn't worth the effort.
Another thing: I personally use CommsChampion Ecosystem at work for the old product that is compiled using quite old gcc-4.8. So I put a significant effort not to introduce code supported by C++14/17/20, but not C++11 or having difficulties being compiled with gcc-4.8. At least not at this stage.
The bottom line, I don't think your suggestion of adding constexpr
to the signature of name()
member function has any (significant) added value.
from commsdsl.
Thanks for the thorough elaboration. You definitely have considered the topic in more depth than I have. I will take a look at my own use of constexpr which until now has been indiscriminate.
from commsdsl.
Related Issues (20)
- Question: List without length and message size HOT 2
- ICD generation HOT 7
- Missing copyright holder in license HOT 5
- ASN.1 Parsing/Serialization HOT 2
- Bit Order in `<bitfield>` and `<set>` HOT 4
- `optional` field with `cond` depending on another `optional` field. HOT 7
- Field Extension Support in Lists with termSuffix HOT 4
- Need Help HOT 21
- Creating Frame with `id` that is less than one byte HOT 3
- Can a (serial) protocol with character escaping be realized using the custom code feature? HOT 2
- Schema name cannot start with a capital HOT 4
- Select variant based on value in the transport frame HOT 1
- noexcept error with gcc 9 HOT 5
- Unexpected max serialisation length HOT 4
- Dead links in the Readme HOT 1
- vcpkg integration HOT 16
- error when using COMMSDSL_NO_TESTS HOT 2
- Undefined behaviour on "out of range" values of ID layer. HOT 4
- "Unexpected max serialisation length" on variable length field inside variant HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commsdsl.