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 

how to avoid to explicitly specify template arguments?

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





PostPosted: Thu Feb 16, 2006 3:06 am    Post subject: how to avoid to explicitly specify template arguments? Reply with quote



Hello,

I am thinking about the following issue. When writing a function template which
takes a parameter I want pass this parameter as efficiently as possible. (e.g.
using some traits template to figure out whether its a primitive type or not).

Something like this comes to my mind:

template<typename T>
void func( Loki::TypeTraits<T>::ParameterType t )
{ /* do something with t */ }

This is written fast, but unfortunately it can't be used so conveniently,
because the dependent type is a non-deduced context. And thus I have to specify
the type as a template argument. I don't want that. OK, the next idea: I could
try to miinimize copying by using a wrapper around the parameter type:

// please: this could also be a wrapper provided by boost or so
template<typename T>
struct MySmallSizedWrapper
{
MySmallSizedWrapper( T const& t ) : pT_( &t ) {}
operator T const& () const { return *pT_; }

private:
T const* pT_;

};

template<typename T>
void func( MySmallSizedWrapper<T> t )
{ /* do something with t */ }

My hope was, that I am now able to pass a T which is then converted to
MySmallSizedWrapper<T> (because I haven't made the constructor explicit), but
this doesn't work because conversions are not applied when matching the function
template.

What I don't want to do is neither to specify the template parameter explicitly
(as before) nor do I want to mention MySmallSizedWrapper when using the function
template and despite all this I wan't to pass the parameter in the most
efficient way. My question at this point is: Is there a way to do this?


thx in advance

Lars

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





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: how to avoid to explicitly specify template arguments? Reply with quote



Lars Jordan wrote:
Quote:
Hello,

I am thinking about the following issue. When writing a function template which
takes a parameter I want pass this parameter as efficiently as possible. (e.g.
using some traits template to figure out whether its a primitive type or not).

Something like this comes to my mind:

template<typename T
void func( Loki::TypeTraits<T>::ParameterType t )
{ /* do something with t */ }

You may create two function templates. One for built-in types, the
other for UDT. Something similar to this:

template<class T>
typename boost::enable_if<boost::is_fundamental<T>, void>::type
foo(T t);

template<class T>
typename boost::disable_if<boost::is_fundamental<T>, void>::type
foo(T const& t);


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





PostPosted: Sat Feb 18, 2006 1:06 am    Post subject: Re: how to avoid to explicitly specify template arguments? Reply with quote



Quote:
What I don't want to do is neither to specify the template parameter explicitly
(as before) nor do I want to mention MySmallSizedWrapper when using the function
template and despite all this I wan't to pass the parameter in the most
efficient way. My question at this point is: Is there a way to do this?

You could make use of the fact that template parameters can me deduced
from function template arguments. Therefore:

#include <iostream>

template<typename T>
struct MySmallSizedWrapper
{
MySmallSizedWrapper( T const& t ) : pT_( &t ) {}
operator T const& () const { return *pT_; }

private:
T const* pT_;

};
template <class T>
MySmallSizedWrapper<T> mySmallSizedWrapper( T const& t )
{
return MySmallSizedWrapper<T>( t );
}

template<typename T>
void func( MySmallSizedWrapper<T> t )
{ /* do something with t */ }

int main()
{
func( mySmallSizedWrapper( 5 ) );
std::cin.get();
return 0;
}

I compiled something similar under VC++ 7.1.3 and it seemed fine.
Usually only one copy of MySmallSizedWrapper would be made (the
temporary required for the argument of func) due to optimizations. The
solution therefore should be efficient.

Regards,

Werner


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





PostPosted: Sun Feb 19, 2006 12:06 pm    Post subject: Re: how to avoid to explicitly specify template arguments? Reply with quote

werasm wrote:

Quote:
You could make use of the fact that template parameters can me deduced
from function template arguments. Therefore:

I know about template argument deduction. Unfortunately it doesn't free me of
the "syntactic noise" when using the 'doSomething()' function. I'd still have to
write down something including 'MySmallSizedWrapper' (which doesn't really
contribute to the problem solution.

On the other hand, the Maxim Yegorushkin's solution _does_ remove this noise
since I don't have to specify anything but the parameters that 'doSomething()'
has to deal with. Nevertheless, thank you very much for your reply.


best regards

Lars

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