C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Tom Tanner
Guest





PostPosted: Fri Jan 23, 2004 9:48 am    Post subject: Template functions Reply with quote



I haven't used templates much before, and I'm a little confused. I've
tried defining a function template of the form

template <class RECORD, class KEY, class POLICY=Policy
lookup(/* parameters */)

And I've had 2 compilers that tell me "Default parameters in template
functions are illegal". The only reference I have access to is
Stroustroup C++ (Special Edition), and he does almost exactly the same
thing on page 340. (Section 13.4.1 Default parameters).

So, why can't I? I'm not paricularly happy about having to set up a
template class with only one operation to achieve this.

Thanks

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Fri Jan 23, 2004 8:57 pm    Post subject: Re: Template functions Reply with quote



In message <22f7de4e.0401220317.57feca60 (AT) posting (DOT) google.com>, Tom Tanner
<ttanner2 (AT) bloomberg (DOT) net> writes
Quote:
I haven't used templates much before, and I'm a little confused. I've
tried defining a function template of the form

template <class RECORD, class KEY, class POLICY=Policy lookup(/* parameters */)

And I've had 2 compilers that tell me "Default parameters in template
functions are illegal".

Well some older compilers will tell you that. Actually there are reasons
why the choice of allowing default template parameters is far from cost
free and when the decision was made (by WG21) there were some less than
happy implementors.

Quote:
The only reference I have access to is
Stroustroup C++ (Special Edition), and he does almost exactly the same
thing on page 340. (Section 13.4.1 Default parameters).

So, why can't I? I'm not paricularly happy about having to set up a
template class with only one operation to achieve this.

I do not understand that last comment. The above code concerns template
functions and writing a simple template overload that forwards to the
full template is hardly onerous:

template returntype lookup(/* parameters */){
return lookup<record, key, Policy }

Please break the habit of using all uppercase names, their use should
signify some form of danger (most commonly that they are pre-processor
names)

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Fri Jan 23, 2004 9:09 pm    Post subject: Re: Template functions Reply with quote





Tom Tanner schrieb:

Quote:
I haven't used templates much before, and I'm a little confused. I've
tried defining a function template of the form

template lookup(/* parameters */)

And I've had 2 compilers that tell me "Default parameters in template
functions are illegal". The only reference I have access to is
Stroustroup C++ (Special Edition), and he does almost exactly the same
thing on page 340. (Section 13.4.1 Default parameters).

The compilers are correct, and the example in TCPPPL is wrong.
You can have default template parameters for class templates, but not for
functions

Quote:


So, why can't I? I'm not paricularly happy about having to set up a
template class with only one operation to achieve this.

I don't know, and yes, I wish the next version of the standard would allow
default template parameters for functions too. To me, the restriction
doesn't make much sense either, but maybe one of those who set up the
rules could tell us more about the reasoning behind it?

regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Gazard
Guest





PostPosted: Sat Jan 24, 2004 1:13 pm    Post subject: Re: Template functions Reply with quote

[email]ttanner2 (AT) bloomberg (DOT) net[/email] (Tom Tanner) writes:

Quote:
I haven't used templates much before, and I'm a little confused. I've
tried defining a function template of the form

template lookup(/* parameters */)

And I've had 2 compilers that tell me "Default parameters in template
functions are illegal". The only reference I have access to is
Stroustroup C++ (Special Edition), and he does almost exactly the same
thing on page 340. (Section 13.4.1 Default parameters).

So, why can't I? I'm not paricularly happy about having to set up a
template class with only one operation to achieve this.

You cannot specify default template parameters in function template
(but you can in class templates). As far as I know this limitation is
mainly the result of historical reasons. When templates were added to
the language, function template arguments were always deductable from
the function call ; in other words it was impossible to explicitly
specify template arguments. From this fact, there were no reason to
add default template arguments at this time (since it had no
sense). Obviously now we can specify explicitly template arguments but
default parameter were never added.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Sat Jan 24, 2004 1:14 pm    Post subject: Re: Template functions Reply with quote

Tom Tanner wrote:
Quote:
template <class RECORD, class KEY, class POLICY=Policy lookup(/* parameters */)

Use overloading:

template
template <class RECORD, class KEY, class POLICY>
void lookup(RECORD &r, KEY &k);

template <class RECORD, class KEY>
void lookup(RECORD &r, KEY &k)
{ lookup<RECORD, KEY, Policy(r, k); }

int main(int c, char **v)
{
lookup(c, v);
lookup<int const, char * const * const>(c, v);
lookup<int, char **, double>(c, v);
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Sat Jan 24, 2004 1:16 pm    Post subject: Re: Template functions Reply with quote


"Thomas Mang" <a9804814 (AT) unet (DOT) univie.ac.at> wrote

Quote:



The compilers are correct, and the example in TCPPPL is wrong.
You can have default template parameters for class templates, but
not for
functions


Bjarne's website explains: Due to an unfortunate oversight, the
standard simply bans default arguments for template parameters for a
function template. Voted to be corrected in the next standard.

See http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#226.

Jonathan



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Vassili Bourdo
Guest





PostPosted: Sat Jan 24, 2004 1:23 pm    Post subject: Re: Template functions Reply with quote

Hello, Tom Tanner!
You wrote on 23 Jan 2004 04:48:53 -0500:

TT> template <class RECORD, class KEY, class POLICY=Policy
TT> lookup(/* parameters */)

TT> And I've had 2 compilers that tell me "Default parameters in template
TT> functions are illegal".
They're illegal indeed.

But you can use overloading...
Like this:

template <class RECORD, class KEY, class POLICY>
lookup(const KEY&, RECORD&, const POLICY&)

//and second overload - for default policy

template <class RECORD, class KEY, class POLICY>
lookup(const KEY& key, RECORD& record)
{
lookup(key, record, Policy<KEY,RECORD>());
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Tom Tanner
Guest





PostPosted: Mon Jan 26, 2004 10:43 am    Post subject: Re: Template functions Reply with quote

Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote

Quote:
In message <22f7de4e.0401220317.57feca60 (AT) posting (DOT) google.com>, Tom Tanner
[email]ttanner2 (AT) bloomberg (DOT) net[/email]> writes
I haven't used templates much before, and I'm a little confused. I've
tried defining a function template of the form

template <class RECORD, class KEY, class POLICY=Policy lookup(/* parameters */)

And I've had 2 compilers that tell me "Default parameters in template
functions are illegal".

Well some older compilers will tell you that. Actually there are reasons
why the choice of allowing default template parameters is far from cost
free and when the decision was made (by WG21) there were some less than
happy implementors.
gcc 3.2 is "an older compiler"?


Quote:
The only reference I have access to is
Stroustroup C++ (Special Edition), and he does almost exactly the same
thing on page 340. (Section 13.4.1 Default parameters).

So, why can't I? I'm not paricularly happy about having to set up a
template class with only one operation to achieve this.

I do not understand that last comment.
Oh dear, wasn't I clear enough Sad ? I said "I haven't used templates

much before".

Quote:
The above code concerns template
functions and writing a simple template overload that forwards to the
full template is hardly onerous:
The solution that was given to me involved writing a template class

and an operator(). That's not entirely nice (IMHO, etc).

Quote:
template returntype lookup(/* parameters */){
return lookup }

Please break the habit of using all uppercase names, their use should
signify some form of danger (most commonly that they are pre-processor
names)
I thought coding style wars were frowned upon in this forum? I'm

trying hard to change the company coding style that requires upper
case for enumerations. Anyway, there are certain advantages to
flagging template parameters in some fashion that indicates to the
reader that the name, when used, is actually a parameter and not an
instance of a variable/class defined elsewhere.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Jan 26, 2004 2:48 pm    Post subject: Re: Template functions Reply with quote

In message <22f7de4e.0401260026.4b9cd7a (AT) posting (DOT) google.com>, Tom Tanner
<ttanner2 (AT) bloomberg (DOT) net> writes
Quote:
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:<fDEGGDCnnPEAFwJv (AT) robinton (DOT) demon.co.uk>...
In message <22f7de4e.0401220317.57feca60 (AT) posting (DOT) google.com>, Tom Tanner
[email]ttanner2 (AT) bloomberg (DOT) net[/email]> writes
I haven't used templates much before, and I'm a little confused. I've
tried defining a function template of the form

template <class RECORD, class KEY, class POLICY=Policy lookup(/* parameters */)

And I've had 2 compilers that tell me "Default parameters in template
functions are illegal".

Well some older compilers will tell you that. Actually there are reasons
why the choice of allowing default template parameters is far from cost
free and when the decision was made (by WG21) there were some less than
happy implementors.
gcc 3.2 is "an older compiler"?

No, the error was mine, I had forgotten that in the end we chose to
allow default template parameters for class templates but not for
function templates. The later could be handled by overloading (actually
the former could be handled by partial-specialisation but we decided to
support default parameters as well).

Quote:
The only reference I have access to is
Stroustroup C++ (Special Edition), and he does almost exactly the same
thing on page 340. (Section 13.4.1 Default parameters).

So, why can't I? I'm not paricularly happy about having to set up a
template class with only one operation to achieve this.

I do not understand that last comment.
Oh dear, wasn't I clear enough Sad ? I said "I haven't used templates
much before".

The above code concerns template
functions and writing a simple template overload that forwards to the
full template is hardly onerous:

The solution that was given to me involved writing a template class
and an operator(). That's not entirely nice (IMHO, etc).

And completely unnecessary AFAICS.

Quote:
template returntype lookup(/* parameters */){
return lookup }

Please break the habit of using all uppercase names, their use should
signify some form of danger (most commonly that they are pre-processor
names)
I thought coding style wars were frowned upon in this forum?

We should, I think, distinguish between pure coding style issues (e.g.
how you format a compound statement) from issues where there are real
problems with a specific style and the use of identifiers that have no
lowercase symbols does increase the risk of pre-processor generated
problems (just as the use of preprocessor identifiers that include a
lowercase letter cause problems to users).

Quote:
I'm
trying hard to change the company coding style that requires upper
case for enumerations.

Good :-)

Quote:
Anyway, there are certain advantages to
flagging template parameters in some fashion that indicates to the
reader that the name, when used, is actually a parameter and not an
instance of a variable/class defined elsewhere.

I remain unconvinced, and definitely unconvinced that risking collision
with the preprocessor is worth it.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tom Tanner
Guest





PostPosted: Mon Jan 26, 2004 8:31 pm    Post subject: Re: Template functions Reply with quote

"Vassili Bourdo" <vassili_bourdo (AT) softhome (DOT) net> wrote

Quote:
Hello, Tom Tanner!
You wrote on 23 Jan 2004 04:48:53 -0500:

TT> template <class RECORD, class KEY, class POLICY=Policy TT> lookup(/* parameters */)

TT> And I've had 2 compilers that tell me "Default parameters in template
TT> functions are illegal".
They're illegal indeed.

But you can use overloading...
Like this:

template <class RECORD, class KEY, class POLICY
lookup(const KEY&, RECORD&, const POLICY&)

//and second overload - for default policy

template lookup(const KEY& key, RECORD& record)
{
lookup(key, record, Policy }

That works fine with gcc. But, guess what. The version of the Sun C++
compiler we use for "official" builds has a bug in it that stops you
overloading inlined functions! Aarrgh!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Attila Feher
Guest





PostPosted: Wed Jan 28, 2004 1:26 pm    Post subject: Re: Template functions Reply with quote

Francis Glassborow wrote:
[SNIP]
Quote:
We should, I think, distinguish between pure coding style issues (e.g.
how you format a compound statement) from issues where there are real
problems with a specific style and the use of identifiers that have no
lowercase symbols does increase the risk of pre-processor generated
problems (just as the use of preprocessor identifiers that include a
lowercase letter cause problems to users).

I'm
trying hard to change the company coding style that requires upper
case for enumerations.

Good :-)

Anyway, there are certain advantages to
flagging template parameters in some fashion that indicates to the
reader that the name, when used, is actually a parameter and not an
instance of a variable/class defined elsewhere.

I remain unconvinced, and definitely unconvinced that risking
collision with the preprocessor is worth it.

Just to back this up a little bit. When we make template examples a type is
usually called T. In a "well known compiler" T is a macro, making text
(character string literal) either wide or normal, depending on if the target
environment is UNICODE or not. Of course, one will get a nasty error for T
in such a case. However I sincerely beliebe that more nasty cases can and
do pop up. Also in a non-trivial project one has to use headers made by
others. You make changes to your code. Try to compile it. You get nasty
error messages. You start looking into the changes you have done - and
notheing is wrong with them, just someone has included a new system/3rd
party header, which in turn redefined one of your words. The problem is not
that the code will silently not function, usually the problem is the time it
takes to figure out where the error message comes from.

--
Attila aka WW



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.