472,365 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,365 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 1607
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: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.