mercredi 18 août 2021

User-defined conversion doesnt work when converting between custom classes

I'm working on a project that uses Dear-Imgui and the math library GLM. Both have their own vector types defined. Dear-ImGui provides a macro however, that let's you define additional constructor and user-defined conversion function so that GLM vector types can be passed in to the ImGui Vector constructors and ImGui vector types can be implicitly converted to GLM vector types.

However I'm facing a problem. Take a look at the following code

glm::vec2 a;
ImVec2 b;
glm::vec2 result = a + b;       // doesn't work. 
result = b                     // works

The error is

"No match for operator+, operand types are glm::vec2 and ImVec2"

Since the last line works this means imgui vector types are being successfully converted to glm. However I don't have a clue as to why the conversion function fails to operate in the arithmetic expression. I thought that the compiler would find that b being incompatible with the glm vector type, also has a conversion function defined and thus would implicitly convert to that and get me the result.

Some key things to note.

  1. Arithmetic operator overloading is not defined for ImGui vector types by default. But they are defined for GLM by default.

  2. If you enable the macro that defines + operator for imgui vector types than the above code works.

Aucun commentaire:

Enregistrer un commentaire