ndsev / zserio

zero sugar, zero fat, zero serialization overhead
https://zserio.org/
BSD 3-Clause "New" or "Revised" License
108 stars 26 forks source link

All generated c++ const literals should be constexpr #509

Closed AntonSulimenkoHarman closed 1 month ago

AntonSulimenkoHarman commented 1 year ago

zs file

const string StringViewShouldBeConstexpr = "ShouldBeConstexpr";
const float64 Float64ShouldBeConstexpr = 0;
...

generated c++ file

/*now*/ const ::zserio::StringView StringViewShouldBeConstexpr = ::zserio::makeStringView("ShouldBeConstexpr");
/*new*/ constexpr ::zserio::StringView StringViewShouldBeConstexpr = ::zserio::makeStringView("ShouldBeConstexpr");

/*now*/ const double Float64ShouldBeConstexpr = 0;
/*new*/ constexpr double Float64ShouldBeConstexpr = 0;

https://github.com/ndsev/zserio/blob/master/test/language/literals/cpp/LiteralsTest.cpp It should be possible to test all literals with static_assert()

mikir commented 1 year ago

Yes, constexpr will be better. Thanks to pointing this out!

mikir commented 2 months ago

We will have to check all consts in the generated code and in the C++ runtime library because of that. It would be good to have constexpr wherever it is possible.

AntonSulimenkoHarman commented 4 weeks ago

Thanks. It works like a charm.