Jump to content
  • Advertisement
Sign in to follow this  
Khatharr

Template Ambiguity

This topic is 624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently have

  template<class T>
  bool vs(const SC::Aggregate& a, const T& b) {
    //WARNING: see bool vs(const SC::Aggregate& a, const SC::Aggregate& b)
    //if this fn is modified
    for(auto& p : a.points)  { if(vs(p, b)) { return true; } }
    for(auto& c : a.circles) { if(vs(c, b)) { return true; } }
    for(auto& r : a.rects)   { if(vs(r, b)) { return true; } }
    return false;
  }

  template<class T>
  bool vs(const T& a, const SC::Aggregate& b) {
    return vs(b, a);
  }

  bool vs(const SC::Aggregate& a, const SC::Aggregate& b) {
    //~~@ ideally this would invoke the first overload instead
    for(auto& p : a.points)  { if(vs(p, b)) { return true; } }
    for(auto& c : a.circles) { if(vs(c, b)) { return true; } }
    for(auto& r : a.rects)   { if(vs(r, b)) { return true; } }
    return false;
  }

I'm wondering if there's a way to express that Aggregate v Aggregate should be handled by the first one there, rather than having to clone the code like this.

Share this post


Link to post
Share on other sites
Advertisement
This looks like a stab at double dispatch; is that correct? There are ways around this but I want to be sure I'm attacking the right issue first :-)

Share this post


Link to post
Share on other sites

It's more a case of not wanting to write the same code in multiple places. It's adjacent to some DD though. One sec, I'll push it and post the github.

 

https://github.com/khatharr/SimpleCollision/blob/master/SimpleCollision/simpleCollision.cpp

https://github.com/khatharr/SimpleCollision/blob/master/SimpleCollision/simpleCollision.h

 

main.cpp is not presentable at the moment.

 

Edit: Actually, yeah, I guess this would count as DD as well, lol. In this specific case I just want to be able to call vs() without looking up argument order.

 

I should mention that I'm aware that this would be a lot better without the polymorphism that is at the root of this madness. I'm planning to factor that out later. At this point I'm specifically exploring this structure to see what kind of weirdness comes out of it. It was something that I had done a long time ago and now that I'm doing my own work again I wanted to look at it with new eyes.

Edited by Khatharr

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!