Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to define a template function so it automatically deduces parameter type


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Misery   Members   -  Reputation: 317

Like
0Likes
Like

Posted 15 October 2012 - 02:40 AM

Hi there,

I do have a bitses matrix class defined as follows:
template <class INT_TYPE,class CONTAINER_INT=uINT,
						  INT_TYPE ContainerSize=sizeof(CONTAINER_INT)*8,
						  class DATA_CONTAINER=BitsetDataContainer<INT_TYPE,CONTAINER_INT,ContainerSize> >
   class BitsetMatrix
   {
	///code
   };

And I have a function:
template <class INT_TYPE>
   INT_TYPE Sum(const BitsetMatrix<INT_TYPE> &M)
   {
	INT_TYPE Rt;
	if (!M.IsEmpty())
	{
		//count sum using another function
	}
	return Rt;
   }

How to define this function that it deduces INT_TYPE argument automatically? In VC++ it works fine, in GCC I need to write:
BitsetMatrix<int> BM;
Sum<int>(BM);

How to define function Sum to avoid <int> in its call.

Regards

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12862

Like
3Likes
Like

Posted 15 October 2012 - 03:30 AM

I only have a passing understanding of templates in C++, but the way I would do it is by making Sum be a template of the matrix type, and the matrix can provide a typedef to access the underlying INT_TYPE.

#3 wqking   Members   -  Reputation: 756

Like
2Likes
Like

Posted 15 October 2012 - 04:22 AM

Quick code, I'm not sure if it's compilable or work, don't have time to test for now.

template <class INT_TYPE,class CONTAINER_INT=uINT,

												  INT_TYPE ContainerSize=sizeof(CONTAINER_INT)*8,

												  class DATA_CONTAINER=BitsetDataContainer<INT_TYPE,CONTAINER_INT,ContainerSize> >

   class BitsetMatrix

   {

   public:

	 typedef INT_TYPE IntType;

		///code

   };





template <class T>

   typename T::IntType Sum(const T &M)

   {

		typename T::IntType Rt;

		if (!M.IsEmpty())

		{

				//count sum using another function

		}

		return Rt;

   }




Edited by wqking, 15 October 2012 - 04:26 AM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#4 Misery   Members   -  Reputation: 317

Like
0Likes
Like

Posted 15 October 2012 - 06:26 AM

@wqking: that looks promising :]

#5 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 16 October 2012 - 03:54 AM

Oh, after reading alvaro's reply again, I just found his opinion is exactly same as my code.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#6 Álvaro   Crossbones+   -  Reputation: 12862

Like
0Likes
Like

Posted 16 October 2012 - 06:52 AM

Oh, after reading alvaro's reply again, I just found his opinion is exactly same as my code.


Of course! I just thought you were implementing my suggestion. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS