• Advertisement
Sign in to follow this  

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

This topic is 1923 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

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Quick code, I'm not sure if it's compilable or work, don't have time to test for now.

[source]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;
}

[/source] Edited by wqking

Share this post


Link to post
Share on other sites
Oh, after reading alvaro's reply again, I just found his opinion is exactly same as my code.

Share this post


Link to post
Share on other sites

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. :)

Share this post


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

  • Advertisement