Tpetra question: why aren't all the enums implemented as strong enums?
Created by: bartgol
@trilinos/tpetra
Current Behavior
All the enums are not strong enums, and hence, are implicitly convertible to integer types.
Motivation and Context
I've been spending quite some time to track a bug, which was ultimately due to calling the constructor of Tpetra::Map with the wrong inputs. In particular, the code was trying to create a 3 element, locally replicated map, which should be done like this:
Tpetra::Map<> map(3,0,comm,Tpetra::LocallyReplicated);
but was instead doing
Tpetra::Map<> map(3,Tpetra::LocallyReplicated,comm);
This is actually creating a globally distributed map, with index base being whateve the enum value LocallyReplicated
is mapped to (in the case of this enum, it is zero). If the enum had been declared as a scoped enum, this would not be possible (unless one really tries to extract the underlying integer), and the compiler would have warned the distracted developer of the mistake.
I am guessing the one reason those enums are not turned into strong enums is because of bwd compatibility (you can't name strong enums without their enum type scope), so quite a few downstream codes would break. However, I'm wondering if there's any plan to change this, perhaps in a future major release, to improve code safety. It seems to me that, unless one is aiming at exploiting the good old enum->int implicit conversion, all the enums should be turned into strong enums for safety reasons...
Possible Solution
Well, the "solution" would be to turn the enum
s in Tpetra_ConfigDefs.hpp
into enum class
es, with all the cascade mods that this would entail. Not a small change by any means.
Steps to Reproduce
The two maps above are a good example of a semantic error due to the lack of type-checking.