wise_enum icon indicating copy to clipboard operation
wise_enum copied to clipboard

Bug compiler does not seem to be correctly resolving wise enum

Open JoshuaSBrown opened this issue 2 years ago • 2 comments

Perhaps my implementation is wrong but I have something like the following:

// myclass.hpp
class MyClass {
  public:
    WISE_ENUM_CLASS_MEMBER(Type, int64_t, MyENUM);
    
  private:
    Type type_ = Type::MyENUM;
    
  public:
    MyClass(const Type type);
    MyClass() : MyClass(type_) {};
}

// myclass.cpp
MyClass::MyClass(const Type type) {
  std::court << "type " << wise_enum::to_string(type) << " and type_ " << wise_enum::to_string(type_) << std::endl;
  type_ = type;
}

Running this with a default instance of MyClass leads to output like so:

int main() {
  MyClass obj;
  return 0;
}
type  and type_ MyENUM

Where there is no string content for type?

JoshuaSBrown avatar Dec 29 '21 18:12 JoshuaSBrown

I was able to get around the issue by explicitly placing the enum value as the argument to nested constructor call here. Though I still think the above should have worked.

MyClass() : MyClass(Type::MyENUM) {};

JoshuaSBrown avatar Dec 29 '21 18:12 JoshuaSBrown

Did you try using a type that prints something in its constructor, instead of an enum, to see if it's actually constructed? What you're doing here is extremely questionable, I think it's UB. When you use a delegating constructor like that, I would expect that member initializers haven't run yet.

quicknir avatar Mar 04 '22 23:03 quicknir