473,796 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterato r 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_ty pe>, 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 1635

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<unsigne d> r = rppower(SafeInt <unsigned>(1) ,
(ptrdiff_t(1))) ;
}

g++ -Wall -c exp.cpp -o exp.o
g++ -o exp exp.o
exp.o(.text+0x2 6): In function `main':
/home/max/src/exp/exp.cpp:24: undefined reference to `SafeInt<unsign ed
int>::SafeInt(u nsigned int)'
exp.o(.text+0x3 f):/home/max/src/exp/exp.cpp:24: undefined reference to
`SafeInt<unsign ed int> rppower<unsigne d int, int>(SafeInt<un signed
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
3205
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 ChainNode { friend Iterator<T>;
1
1438
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); template <typename T> void deallocate(T**** mem); When i try to compile code like: double ***image= allocate<double>(bands, rows, columns);
5
1970
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 function name: void MyFunction(int x, int y); Then when he instantiates a class he calls it: MyClass myclass; myclass (x, y);
4
3217
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 going to teach) , I declared the operator outside the class definition. This would prevent the operator from accessing the private elements within the class, so I provided access to elements of the private vector array thru a public member...
2
3891
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
4692
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 is a template argument), and an int. The second takes a T reference and two ints. Here's the code: ================= #include <iostream>
5
1677
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 call func("some text") then size of the text will be known at compile time instead of doing strlen() I'm sure that long time ago I got it working somehow, but now when I needed and implemented a similar solution I see that it doesn't work.
1
4039
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 overload have a best conversion error C2668: 'swap' : ambiguous call to overloaded function ------------------------------------------------------------------------------------------------------------- template < typename T > void swap(T &lhs, T...
3
6408
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 function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void foo(OutputType*) bla.cxx:10: note: void foo(PixelType*)
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.