Overloading with Scoped Enums and underlying Integer type

Hello!

Ive run into an interesting a particular issue with Scoped Enums.

I have a function overloaded using a scoped enum and its matching underlying integer type.

enum class _enum: uint8_t{
  A_Enum = 0,
  B_Enum = 1,
  C_Enum = 2,
};

uint8_t function(_enum val){
  return 0;
}

uint8_t function(uint8_t val){
  return 1;
}

void setup(){
  Serial.begin(115200);
}

void loop(){
  _enum value = _enum::B_Enum;

  Serial.print("Enum Arguement:    "); Serial.println(function(value));
  Serial.print("Integer Arguement: "); Serial.println(function(1));
  
  delay(2000);
}

This same code compiles fine on Arduino IDE and Platform.io (gcc-arm compiler).
But I get a “redeclaration of function” error for the Particle Compile.

Is this just a difference between compilers? or is this an issue with the C++ standard on the Particle compile. It seems to me that the point of having a scoped enum is to seperate it from the underlying integer value, so you should be able to overload based on it.

Thanks for the help!

1 Like

I don’t know the answer to your question, but the current cloud compilers use gcc 5.3.1 in C++17 mode. This is a pretty old version of the compiler.

The next version of Device OS (2.0.0) will use gcc 9.2.1 (also in C++17 mode). It may behave differently.

1 Like

So apparently it’s a bug in the Wiring pre-processor. If you are using the Workbench or CLI compilers, changing the .ino file to be .cpp disables the Wiring pre-processor.

In the Web IDE, you can add this to the top of the .ino file:

#pragma SPARK_NO_PREPROCESSOR
2 Likes

Thanks! That works for now.

Ill be looking forward to a fix in future version.

Issue on github.com/particle-iot/device-os

EDIT: I have editing this message after discovering that the issue inside the library stemed from another problem. My solution to that problem can be found here. The issue inside the library seemed to be a difference in how the two version of gcc compiled the code. The gcc version used by particle seems to not like function defined in Header files. Moving the definitions to a .cpp file solved the issue.

Original post for reference:

Sorry, It seems this work-around doesnt work when dealing with libraries.

My large library with the same format of functions continues to have this issue.
Does the Wiring pre-processor reach into the library's source files and make similair changes?

Compile Error (repeated for various enums used):

../../../build/target/user/platform-6-m/Particle_Test_Project/\libuser.a(SOURCE_FILE.o): In function `nmspcA::nmspcB::function(unsigned char)':
a:\path\to\file\file.h:24: multiple definition of `nmspcA::nmspcB::function(unsigned char)'
../../../build/target/user/platform-6-m/Particle_Test_Project/\libuser.a(PROJECT_CPP.o):a:\path\to\file\file.h.h:24: first defined here
../../../build/target/user/platform-6-m/Particle_Test_Project/\libuser.a(SOURCE_FILE.o): In function `nmspcA::nmspcB::function(unsigned char)':

One of these functions uses a Scoped Enum as an argument, same as the example in original post