curve
curve copied to clipboard
Some encoding in MetaServer is affected by C++ DR1601
DR1601 fixed an overload problem when an enumeration has an underlying type. https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1601
A conversion that promotes an enumeration whose underlying type is fixed to its underlying type is better than one that promotes to the promoted underlying type, if the two are different.
And GCC implements it in GCC-10, clang in clang-10, so our code will be broken when we upgrade into higher versions.
Here is an example to demonstrate this,
#include <iostream>
#include <string>
#include <sstream>
enum Foo : unsigned char {
kHello = 1,
kWorld = 2
};
std::string format(Foo f) {
std::ostringstream oss;
oss << f;
return oss.str();
}
int main() {
auto res = format(kHello);
std::cout << '>' << res << '<' << std::endl;
std::cout << std::hash<std::string>{}(res) << std::endl;
std::cout << '>' << kHello << '<' << std::endl;
return 0;
}
https://godbolt.org/z/Yj8zad1rz
GCC-6.3 (we are using) and clang-3.8 output
>1<
10159970873491820195
>1<
GCC-10 and clang-10 output
><
4334672815104069193
><
Our code has the same logic and assumes the output is >1<
when decoding.
A feasible way is removing the underlying type from enumeration definition, or explicit cast it into int when encoding.
so if update the compiler version, will there be more warnings ? but does it affect normal operation? or affects performance?
so if update the compiler version, will there be more warnings ?
I didn't see warnings about this, but, some unit tests failed when compiling with a higher version compiler.
but does it affect normal operation? or affects performance?
I don't think so, but about performance, our encoding uses std::ostringstream
, and many tests show its drawback on performance. absl::StrCat
is a good candidate, in this demo, it's 5x-10x faster than std::ostringstream
.