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 

Q: Eliminate explicit template function argument

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





PostPosted: Tue Aug 26, 2003 12:44 am    Post subject: Q: Eliminate explicit template function argument Reply with quote



Hi there,

I have a function similar to this:

template <typename T>
void someFunc (someClass<T> const& arg)
{
/* ... */
}

The thing is, 90% of the time 'T = char' and the argument get
instantiated from a 'char const*'.

So that would yeild the following syntax:

someFunc(someClass<char>("foo"));

We could set "T = char" in the definition of someClass (for
the records):

template <typename T = char>
struct someClass
{
someClass (const char* name);
/* ... */
};

That would reduce the syntax to the following:

someFunc(someClass<>("foo"));

Or perhaps better, we could specify the explicit 'char' type
when calling someFunc, which would instead give us:

someFunc<char>("foo");

The question now is, is there anyway we can get it down to
simply:

someFunc("foo");

I realize that I could do a special 'char const*' overload for
someFunc, but there are many functions which take the
'someClass<T> const&' as argument, thus I would need to
overload all of these (which makes me favor a way in which the
compiler would be able to fill in the type, unfortunately
default types for template functions are not allowed).

Regards Allan

[ 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: Tue Aug 26, 2003 6:41 pm    Post subject: Re: Eliminate explicit template function argument Reply with quote



Allan Odgaard wrote:
Quote:
Hi there,

I have a function similar to this:

template <typename T
void someFunc (someClass {
/* ... */
}

The thing is, 90% of the time 'T = char' and the argument get
instantiated from a 'char const*'.

So that would yeild the following syntax:

someFunc(someClass<char>("foo"));

[SNIP]
The question now is, is there anyway we can get it down to
simply:

someFunc("foo");

I realize that I could do a special 'char const*' overload for
someFunc, but there are many functions which take the
'someClass<T> const&' as argument, thus I would need to
overload all of these (which makes me favor a way in which the
compiler would be able to fill in the type, unfortunately
default types for template functions are not allowed).

How about specializations for "T const *" and "T const &"? That is two full
specializations of the function template and it seems to solve your issue to
get rid of the const and the ref or ptr from the T type.

--
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
Allan Odgaard
Guest





PostPosted: Wed Aug 27, 2003 10:14 pm    Post subject: Re: Eliminate explicit template function argument Reply with quote



Attila Feher wrote:

Quote:
I have a function similar to this:
template <typename T
void someFunc (someClass [...] is there anyway we can get it down to
someFunc("foo");

How about specializations for "T const *" and "T const &"? That is two full
specializations of the function template and it seems to solve your issue to
get rid of the const and the ref or ptr from the T type.

The T type is never a pointer or reference, so I don't really see how
this relates to my problem. Could you provide me with an example?

Thanks in advance, Allan

[ 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: Thu Aug 28, 2003 8:40 pm    Post subject: Re: Eliminate explicit template function argument Reply with quote

Allan Odgaard wrote:
Quote:
Attila Feher wrote:

I have a function similar to this:
template <typename T
void someFunc (someClass [...] is there anyway we can get it down to
someFunc("foo");

How about specializations for "T const *" and "T const &"? That is
two full specializations of the function template and it seems to
solve your issue to get rid of the const and the ref or ptr from the
T type.

The T type is never a pointer or reference, so I don't really see how
this relates to my problem. Could you provide me with an example?

I did not pay enough attention I guess. I must remind myself not to post
when having a headache...

template <typename T>
void someFunc (someClass<T> const& arg)
{
/* ... */
}

I guess you will need some sort of overload(?) full specialization(?)
(Andrei Help!) of this template with a template template parameter. But
then again I might be wrong... In that case of course you will need
overloads (pr specializations) for all kinds of templates you want to pass
(I mean taking 1,2,3... parameters).

--
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.