473,395 Members | 1,797 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,395 software developers and data experts.

template functions

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
Jul 23 '05 #1
3 1683
REH

"REH" <me@you.com> wrote in message
news:Pm***************@twister.nyroc.rr.com...
Is the second set of definitions legal?


I, of course, meant to say "declarations."

Jul 23 '05 #2
"REH" <me@you.com> wrote in message
news:Pm***************@twister.nyroc.rr.com
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.

--
John Carson

Jul 23 '05 #3
REH

"John Carson" <jc****************@netspace.net.au> wrote in message
news:42***********************@un-2park-reader-01.sydney.pipenetworks.com.au...
"REH" <me@you.com> wrote in message
news:Pm***************@twister.nyroc.rr.com
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++.)
Thank you. That's very helpful. I guess I'll send them a bug report.
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.

Yes, I plan to always call the functions with all template parameters.

I appreciate the help.

Jul 23 '05 #4

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

Similar topics

4
by: velthuijsen | last post by:
I've been reading (and using the examples in) Thinking in C++ (vol 2). One of the things that the author shows you can do with templates is the following: #include <iostream> using namespace...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
9
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? ...
2
by: Drew McCormack | last post by:
I have a self written Tensor class which I need to write a number of elementwise operations for (eg sin, cos, abs, conj). I am trying to implement most of these in terms of standard library...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: vectorizor | last post by:
Hello all, I am attempting to vectorize few template functions with the Intel compiler, but without much success so far. Ok granted, this question is not 100% c++, but it is related enough that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
0
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,...
0
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...
0
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,...

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.