473,387 Members | 1,530 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,387 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 1682
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.