template functions

R

REH

I need to create template functions with the same name but different number
of template parameters. My compiler says this is illegal:

template<class TO, class F1>
TO convert_to();

template<class TO, class F1, class F2>
TO convert_to();

template<class TO, class F1, class F2, class F3>
TO convert_to();

But it will accept this:

template<class TO, class F1>
TO convert_to(int = 0);

template<class TO, class F1, class F2>
TO convert_to(int = 0, int = 0);

template<class TO, class F1, class F2, class F3>
TO convert_to(int = 0, int = 0, int = 0);

Is the second set of definitions legal?

Thanks,

REH
 
J

John Carson

REH said:
I need to create template functions with the same name but different
number of template parameters. My compiler says this is illegal:

template<class TO, class F1>
TO convert_to();

template<class TO, class F1, class F2>
TO convert_to();

template<class TO, class F1, class F2, class F3>
TO convert_to();

Your compiler is wrong. These declarations are legal. (I haven't troubled to
find the relevant section of the C++ standard, but Lippman and Lojoie in the
3rd edition of C++ Primer give an example of an overload like yours on p.
521 and your code compiles successfully on both Comeau and VC++.)
But it will accept this:

template<class TO, class F1>
TO convert_to(int = 0);

template<class TO, class F1, class F2>
TO convert_to(int = 0, int = 0);

template<class TO, class F1, class F2, class F3>
TO convert_to(int = 0, int = 0, int = 0);

Is the second set of definitions legal?

Yes.

You should be aware that sometimes function overloading can be legal, but it
may lead to ambiguities in use, leading to a compilation failure (e.g., if
you defined *both* sets of your functions and called convert_to with no
arguments, then a function from each set would match, giving ambiguity).
However, assuming you only define one set, ambiguity should not be a problem
in your case given that you will have to specify all template arguments
explicitly.
 
R

REH

John Carson said:
Your compiler is wrong. These declarations are legal. (I haven't troubled to
find the relevant section of the C++ standard, but Lippman and Lojoie in the
3rd edition of C++ Primer give an example of an overload like yours on p.
521 and your code compiles successfully on both Comeau and VC++.)

Thank you. That's very helpful. I guess I'll send them a bug report.
Yes.

You should be aware that sometimes function overloading can be legal, but it
may lead to ambiguities in use, leading to a compilation failure (e.g., if
you defined *both* sets of your functions and called convert_to with no
arguments, then a function from each set would match, giving ambiguity).
However, assuming you only define one set, ambiguity should not be a problem
in your case given that you will have to specify all template arguments
explicitly.
Yes, I plan to always call the functions with all template parameters.

I appreciate the help.
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top