Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloaded template function

Reply
Thread Tools

Overloaded template function

 
 
Dave Corby
Guest
Posts: n/a
 
      10-12-2005
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
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      10-12-2005

Dave Corby wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template non type parameters in operator overloaded function aravindap C++ 2 10-24-2008 03:56 AM
ambiguous call to overloaded template function Ruki C++ 1 06-30-2008 08:00 AM
Help Required: Operator overloaded function template and Friend CoolPint C++ 3 02-13-2005 04:49 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments