Overloaded template function

D

Dave Corby

Hi all,

I have an overloaded template function, and in one particular spot can't
get the right version of it to be called. Everywhere else in the program
the correct version is called. Here's the function declarations:

template <class NumT, class ExpT>
NumT rppower (NumT const x, ExpT const y);
template <class NumT, class ExpT>
SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);
template <class NumT, class ExpT>
SafeInt<NumT> rppower (SafeInt<NumT> const x, ExpT const y);
template <class NumT, class ExpT>
SafeInt<NumT> rppower (NumT const x, SafeInt<ExpT> const y);

And here's the offending call and containing function. The function is
abbreviated, but the relevant parts should be there:

template <class T> T ArbInt::Value () const {
SafeInt<T> retval;
// _DigsT is vector<unsigned int>
for (typename _DigsT::iterator i = _digits->begin();
i != _digits->end(); ++i) {
retval += *i * rppower(SafeInt<T>(s_digitbase),
(i - _digits->begin()));
}
return retval;
|

For some reason, the first version of rppower is always called, with
NumT being a SafeInt<T>. SafeInt has a casting operator to T's type, but
that shouldn't be being called if there's a better match, and in other
areas it's called correctly. I've tried explicitly specifying rppower<T,
_DigsT::size_type>, I've tried making both arguments to rppower a
SafeInt, and I've even tried making the first version of rppower the
last one declared. Oh, and the * operator is overloaded for unsigned int
* a SafeInt.

I know it's a crazy mess of overloaded functions and operators, but
maybe something will stick out. Here's hoping.

Thanks!
- Dave
 
M

Maxim Yegorushkin

Dave said:
Hi all,

I have an overloaded template function, and in one particular spot can't
get the right version of it to be called. Everywhere else in the program
the correct version is called. Here's the function declarations:
[]

I know it's a crazy mess of overloaded functions and operators, but
maybe something will stick out. Here's hoping.

I made your code compilable and it calls the proper overload as one
would expect.

#include <cstddef>

template<class T> struct SafeInt
{
SafeInt(T);
};

template <class NumT, class ExpT>
NumT rppower (NumT const x, ExpT const y);

template <class NumT, class ExpT>
SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);

template <class NumT, class ExpT>
SafeInt<NumT> rppower (SafeInt<NumT> const x, ExpT const y);

template <class NumT, class ExpT>
SafeInt<NumT> rppower (NumT const x, SafeInt<ExpT> const y);

int main()
{
SafeInt<unsigned> r = rppower(SafeInt<unsigned>(1),
(ptrdiff_t(1)));
}

g++ -Wall -c exp.cpp -o exp.o
g++ -o exp exp.o
exp.o(.text+0x26): In function `main':
/home/max/src/exp/exp.cpp:24: undefined reference to `SafeInt<unsigned
int>::SafeInt(unsigned int)'
exp.o(.text+0x3f):/home/max/src/exp/exp.cpp:24: undefined reference to
`SafeInt<unsigned int> rppower<unsigned int, int>(SafeInt<unsigned
int>, int)'

Note the second undefined reference - it tells you which overload has
been selected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top