zserio icon indicating copy to clipboard operation
zserio copied to clipboard

All generated c++ const literals should be constexpr

Open AntonSulimenkoHarman opened this issue 2 years ago • 2 comments

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()

AntonSulimenkoHarman avatar Jun 01 '23 17:06 AntonSulimenkoHarman

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

mikir avatar Jun 02 '23 08:06 mikir

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.

mikir avatar Jun 18 '24 07:06 mikir

Thanks. It works like a charm.

AntonSulimenkoHarman avatar Aug 09 '24 13:08 AntonSulimenkoHarman