How to design a vector class to use any sqrt function?

Started by
23 comments, last by johnstanp 15 years, 9 months ago
Quote:Original post by the_edd
Quote:Original post by johnstanp
I had problems understanding the traits mechanism which I thought was related to type information...

It usually is used for that, but that's not the heart of the concept. Traits allow you to use 3rd party code with yours without changing the 3rd party implementation. Concept maps in the next revision of the standard takes this idea even further.

The other plus of this is that you don't need to add an extra warty template parameter to your vector class.


I didn't pay close attention to the mechanism you provided. Actually, depending of the type parameter of my vector class, a template specialization would be chosen: so I won't need passing a pointer. The problem is that I want too, to be able to use std::sqrt() which gives better results thant my sqrt algorithm. As the std::sqrt() uses the same types as my function, if I am not mistaken, I won't be able to use that mechanism.

Advertisement
Quote:Original post by johnstanp
Quote:Original post by the_edd
Quote:Original post by johnstanp
I had problems understanding the traits mechanism which I thought was related to type information...

It usually is used for that, but that's not the heart of the concept. Traits allow you to use 3rd party code with yours without changing the 3rd party implementation. Concept maps in the next revision of the standard takes this idea even further.

The other plus of this is that you don't need to add an extra warty template parameter to your vector class.


I didn't pay close attention to the mechanism you provided. Actually, depending of the type parameter of my vector class, a template specialization would be chosen: so I won't need passing a pointer. The problem is that I want too, to be able to use std::sqrt() which gives better results thant my sqrt algorithm. As the std::sqrt() uses the same types as my function, if I am not mistaken, I won't be able to use that mechanism.


So you need template specialisation?

template <class T>class Vector3{public:	T x, y, z;	// generic empty method	T length (void) { return T(); }};// integer square root functionint iSqrt (int x){	/* calculate integer sqrt */	return x;}// implementation for Vector3 <int>template <> int Vector3 <int>::length (void){	return iSqrt (x * x + y * y + z * z);}// implementation for Vector3 <float>template <> float Vector3 <float>::length (void){	return std::sqrt (x * x + y * y + z * z);}
Quote:Original post by johnstanp
Quote:Original post by lexs
If i understand you correctly you want something like this:


*** Source Snippet Removed ***


That's it!


A template function seems more flexible :

template<typename T>class Vector3{	public:		...                template<typename Math>		T length() const		{                        return Math::sqrt(...);		}			private:		T x_[3];};class StdMath{public:	static float sqrt(float x)	{		return std::sqrt(x);	}};Vector<float> Vector;float v = Vector::length<StdMath>();
Quote:Original post by Moomin

A template function seems more flexible :

*** Source Snippet Removed ***


Thank you very much Moomin!!!
That's exactly what I wanted.

Thank you guys for your precious help!!!

PS:well, thre3dee, I can specialize member functions of a template class?
It seems that GCC doesn't allow it, or have I done something incorrectly?

[Edited by - johnstanp on July 7, 2008 9:31:01 PM]
Quote:Original post by johnstanp
Quote:Original post by Moomin

A template function seems more flexible :

*** Source Snippet Removed ***


Thank you very much Moomin!!!
That's exactly what I wanted.

Thank you guys for your precious help!!!

PS:well, thre3dee, I can specialize member functions of a template class?
It seems that GCC doesn't allow it, or have I done something incorrectly?


I haven't used GCC but I assumed template specialisation was part of standard C++; amirong?
Quote:Original post by thre3dee
I haven't used GCC but I assumed template specialisation was part of standard C++; amirong?


Actually, I cannot specialize a member function of a class template without specializing the whole class. I've got various compile errors, that informs me that all member variables have incomplete types.

[Edited by - johnstanp on July 9, 2008 11:49:10 AM]
Quote:Original post by johnstanp
Quote:Original post by thre3dee
I haven't used GCC but I assumed template specialisation was part of standard C++; amirong?


Actually, I cannot specialize a member function of a class template without specializing the whole class. I've got various compile errors, that informs me that all member variables have incomplete types.


That's right. You can't specialize a member function of a class template because you can make a specialization of this class template that doesn't contain that function.
Quote:Original post by Antheus
As for sqrt() function, you can't, that one can only be evaluated during run-time.
You find the below template meta-programming of mine interesting:
It provides compile-time C_FLOORSQRT(n) and C_CEILSQRT(n).
#define C_FLOORSQRT(n) (floorSqrtn<n>::ret)template <int n, int a = (n>>1), int b = -1>struct floorSqrtn {	enum { ret = a*a <= n && n < (a+1)*(a+1) ? a : floorSqrtn<n, ((n/((n/a + a)>>1) + ((n/a + a)>>1))>>1), a>::ret };};template <int n, int a>struct floorSqrtn<n, a, a> {	enum { ret = a };};template <>struct floorSqrtn<1, 0, -1> {	enum { ret = 1 };};#define C_CEILSQRT(n) (ceilSqrtn<n>::ret)template <int n, int a = (n>>1), int b = -1>struct ceilSqrtn {	enum { ret = (a-1)*(a-1) < n && n <= a*a ? a : ceilSqrtn<n, ((n/((n/a + a)>>1) + ((n/a + a)>>1))>>1), a>::ret };};template <int n, int a>struct ceilSqrtn<n, a, a> {	enum { ret = a };};template <>struct ceilSqrtn<1, 0, -1> {	enum { ret = 1 };};
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by Moomin
Quote:Original post by johnstanp
Quote:Original post by lexs
If i understand you correctly you want something like this:


*** Source Snippet Removed ***


That's it!


A template function seems more flexible :

*** Source Snippet Removed ***


I think that would lead to inconsistent usage. I know I'd get annoyed at having to supply a template parameter every time I wanted to do something simple like get a vectors length.

The overhead with traits is a one-time upfront cost for each type that needs to be supported (and the default implementation will suffice for the majority of uses).

I can't deny that they're complicated, but it's the best tool for the job, here, IMHO.
Quote:Original post by johnstanp
Quote:Original post by the_edd
Quote:Original post by johnstanp
I had problems understanding the traits mechanism which I thought was related to type information...

It usually is used for that, but that's not the heart of the concept. Traits allow you to use 3rd party code with yours without changing the 3rd party implementation. Concept maps in the next revision of the standard takes this idea even further.

The other plus of this is that you don't need to add an extra warty template parameter to your vector class.


I didn't pay close attention to the mechanism you provided. Actually, depending of the type parameter of my vector class, a template specialization would be chosen: so I won't need passing a pointer.


I don't know what you mean by "passing a pointer". There are no pointers involved in the code I posted. All functions are class-scope static.

Quote:The problem is that I want too, to be able to use std::sqrt() which gives better results thant my sqrt algorithm. As the std::sqrt() uses the same types as my function, if I am not mistaken, I won't be able to use that mechanism.


By default std::sqrt is used (as it's called by the primary template). It does exactly what you want.

This topic is closed to new replies.

Advertisement