473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloaded template function

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
Oct 12 '05 #1
1 1614

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.

Oct 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roy Yao | last post by:
Why the following code let my compiler complain an overloaded function Init()? // code begin template<class T> class BicircularList { template<class T> class Iterator; template<class T> class...
1
by: Jef Driesen | last post by:
I have 4 overloaded functions 'deallocate': template <typename T> void deallocate(T* mem); template <typename T> void deallocate(T** mem); template <typename T> void deallocate(T*** mem);...
5
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit...
4
by: gobis | last post by:
Hello everyone, After I could not overload the << operator as a friend operator in my template class (yes, it did not have to be a template class, but I was testing something for the class I was...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
2
by: mrstephengross | last post by:
Hi folks! I'm trying to create a function pointer to a templatized, static, overloaded member function. My class (Mgr) has two functions with the same name: "go". The first takes a T reference (T...
5
by: __PPS__ | last post by:
Hello everybody, I'm not sure but I thought that sometime ago I had class with two overloaded methods: void func(const char*); and template<int N>func(const char (&arr)); so, that whenever I...
1
by: Ruki | last post by:
I want to overload function to swap two others types, for example, type<T>, T* and so on, but I can't compile the following code at VC 6.0. The compiler says : error C2667: 'swap' : none of 2...
3
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.