Ambiguous call to overloaded function

J

Joseph Turian

I have a templated class with the following methods:
Vocab(const T& t);
Vocab(unsigned uid);

However, when T = unsigned, and I call Vocab(unsigned(0)) then the
compiler rightly complains about an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?

Thanks,
Joseph
 
S

Salt_Peter

I have a templated class with the following methods:
Vocab(const T& t);
Vocab(unsigned uid);

However, when T = unsigned, and I call Vocab(unsigned(0)) then the
compiler rightly complains about an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?

Thanks,
Joseph


One way is by using a dummy variable to change the member function's
signature
ie:
template < typename T >
class Test
{
...
public:
void Vocab(const T& t);
void Vocab(unsigned u, int dummy);
};

The better way might be to reduce the confusion:

template < typename T >
class Test
{
...
public:
void setT(const T& t);
void setuid(unsigned u);
};
 
I

Ioannis Gyftos

The better way might be to reduce the confusion:

I believe that he wants to do it in a constructor.
However, when T = unsigned, and I call Vocab(unsigned(0)) then the
compiler rightly complains about an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?

Hmm, given that one is const and the other is not, does the explicit
keyword work?
 
I

Ian Collins

Joseph said:
I have a templated class with the following methods:
Vocab(const T& t);
Vocab(unsigned uid);

However, when T = unsigned, and I call Vocab(unsigned(0)) then the
compiler rightly complains about an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?
Call it indirectly through a function template.

The most specialised function matches first, so a regular function
matches before a function template.
 
T

tragomaskhalos

Call it indirectly through a function template.

The most specialised function matches first, so a regular function
matches before a function template.

Clever, but pity the poor maintenance programmer !
Giving the functions different names is less elegant but better for
posterity ...
 

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

Staff online

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top