S
shuisheng
Dear All,
Assume I have two templates as follows:
//! Add #1: p3 = p1 + p2.
template<class _T1, class _T2, class _T3>
void Add(const _T1 *p1, const _T2 *p2, _T3 *p3, size_t n)
{
for (size_t i = 0; i < n; ++i) p3 = p1 + p2[2];
}
//! Add #2: p3 = p1 + d2.
template<class _T1, class _T2, class _T3>
void Add(const _T1 *p1, const _T2 d2, _T3 *p3, size_t n)
{
for (size_t i = 0; i < n; ++i) p3 = p1 + d2;
}
Where p1, p2, p3 are pointers pointing to a group of data, and d2 is
just a single data.
I test the following case:
//! A test.
int *p1 = new int [3]; p1[0] = 1; p1[0] = 2; p1[0] = 3;
int *p2 = new int [3]; p2[0] = 1; p2[0] = 2; p2[0] = 3;
int *p3 = new int [3];
Add(p1, p2, p3, 3);
And I find that the "Add(p1, p2, p3, 3)" tries to call the second
template and givea a compilor error like can not convert int* to int.
In fact the first template can also allow the calling and it will give
the right results. I can change the seconde template name to "Add2" to
get it right. Is there any better way to accomplish it.
Thanks,
Shuisheng
Assume I have two templates as follows:
//! Add #1: p3 = p1 + p2.
template<class _T1, class _T2, class _T3>
void Add(const _T1 *p1, const _T2 *p2, _T3 *p3, size_t n)
{
for (size_t i = 0; i < n; ++i) p3 = p1 + p2[2];
}
//! Add #2: p3 = p1 + d2.
template<class _T1, class _T2, class _T3>
void Add(const _T1 *p1, const _T2 d2, _T3 *p3, size_t n)
{
for (size_t i = 0; i < n; ++i) p3 = p1 + d2;
}
Where p1, p2, p3 are pointers pointing to a group of data, and d2 is
just a single data.
I test the following case:
//! A test.
int *p1 = new int [3]; p1[0] = 1; p1[0] = 2; p1[0] = 3;
int *p2 = new int [3]; p2[0] = 1; p2[0] = 2; p2[0] = 3;
int *p3 = new int [3];
Add(p1, p2, p3, 3);
And I find that the "Add(p1, p2, p3, 3)" tries to call the second
template and givea a compilor error like can not convert int* to int.
In fact the first template can also allow the calling and it will give
the right results. I can change the seconde template name to "Add2" to
get it right. Is there any better way to accomplish it.
Thanks,
Shuisheng