I have the following code that I think should compile but doesn't.
#include <cstdint>
typedef uint8_t testtype;
enum testenum : uint8_t {
};
void foo(uint8_t& v) {}
int main() {
testtype bar;
foo(bar);
testenum baz;
foo(baz);
return 0;
}
I get the following error:
prog.cpp:15:9: error: invalid initialization of non-const reference of type 'uint8_t& {aka unsigned char&}' from an rvalue of type 'uint8_t {aka unsigned char}'
foo(baz);
In my mind, this should work because everything is of the same underlying type (uint8_t). A typedef'ed variable can be passed to the foo function, but the enum (which has enum class uint8_t) cannot.
What this means in my project is that I can't pass all of my enums to a single overloaded function -- it looks like I have to create an overload for every possible enum.
Is there an elegant way to get this to compile, or do I have to pass the enum through a second variable that I can pass by reference?
testenumcan convert to and fromuint8_t, but they are not the same. What you are seeing is that the result of the conversion is an rvalue which cannot be bound to non const reference.