Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

[C++] Using macros to eliminate duplicate code...

This topic is 3814 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 am working on a 3D math library and I have a great deal of code that is very similar, mainly across my vector2<>, vector3<> and vector4<> classes. To deal with this I thought about using a series of macros to define common operations. For example:
#define ASSIGNMENT_OP(CLASS, COUNT)
        CLASS & operator=(const CLASS & rhs)
        { 
             for(int i = 0; i < COUNT; ++i)
                  (*this).data = rhs.data;
             return *this;
        } 

template<typename T>
class vector2
{
  ASSIGNMENT_OP(vector2, 2);
};

template<typename T>
class vector3
{
  ASSIGNMENT_OP(vector3, 3);
};

template<typename T>
class vector4
{
  ASSIGNMENT_OP(vector4, 4);
};

#undef ASSIGNMENT_OP



The problem is that I do not want to use macros unless I feel there is no other choice. I've tried to come up with methods involving inheritance or templates, but I keep coming back to the macro solution.

Share this post


Link to post
Share on other sites
Advertisement
Writing a math library involves annoying amounts of code repetition, and then you're done and don't have to look at it anymore. As a piece of code, it's too trivial to waste much time on. Your macro thing looks fine, if it's what you want to do. Personally, I'd just copy and paste.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
I just use template < typename T , size_t N >
QFE.

Of course you could say that macros don't eliminate duplicated code, they just hide it.

Share this post


Link to post
Share on other sites
Debugging math with templates and macros all over the place is a pain in the ass. Even though they do the same thing, a copy-paste of a typical getter/setting/operator doesn't take much longer and makes things far more usable in the future. Also, although a template like Vector<Type, N> is nice on paper, each type of float, double, int and each size of 2, 3, 4 has their own specific optimizations, precision adjustments, constructors and rules that make them messy to mush together. I keep each variant in a separate class like Vector2f, Vector3f, Vector4f, ...d, ...i, etc, and it's very nice and clean. The same goes for Matrix#x#f, Quaternion#f, EulerAngle#f, AxisAngle#f, etc. *

* My opinion

Share this post


Link to post
Share on other sites
Quote:
Original post by hh10k
Also, although a template like Vector<Type, N> is nice on paper, each type of float, double, int and each size of 2, 3, 4 has their own specific optimizations, precision adjustments, constructors and rules that make them messy to mush together.


That's what specialization is for.

Share this post


Link to post
Share on other sites
The template answer is the "right" answer I would probably go with the copy and paste answer though just because templates make really ugly and annoying compiler errors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by hh10k
Also, although a template like Vector<Type, N> is nice on paper, each type of float, double, int and each size of 2, 3, 4 has their own specific optimizations, precision adjustments, constructors and rules that make them messy to mush together.

That's what specialization is for.

If you're going to specialize all instantiations, why would you template in the first place?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Zahlman
Quote:
Original post by hh10k
Also, although a template like Vector<Type, N> is nice on paper, each type of float, double, int and each size of 2, 3, 4 has their own specific optimizations, precision adjustments, constructors and rules that make them messy to mush together.

That's what specialization is for.

If you're going to specialize all instantiations, why would you template in the first place?


Because it's C++! Why wouldn't you use templates?

Just kidding, in case anybody didn't get it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Zahlman
Quote:
Original post by hh10k
Also, although a template like Vector<Type, N> is nice on paper, each type of float, double, int and each size of 2, 3, 4 has their own specific optimizations, precision adjustments, constructors and rules that make them messy to mush together.

That's what specialization is for.

If you're going to specialize all instantiations, why would you template in the first place?


It's nice to be able to use math::vector<big_float, 3> when needed, or any other type that "implements" enough of the field axioms.

Edd

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!