 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Charles Bissonnette Guest
|
Posted: Sat Aug 06, 2005 11:17 am Post subject: Elide template argument for object instantiation.... |
|
|
I think this is just an annoyance of the language and I was wondering if
there's any workaround.
Supposed I have this object that I declare using a template class.
template<class _fun>
class RangeFilter {
public:
RangeFilter(_fun fun) {...}
.....
.....
};
And then I used an instance of it :
RangeFilter<std::pointer_to_unary_function
rf(std::ptr_fun(myptrFunction));
What I find redundant is to specifiy the template argument of the object
everytime I define an instance. Since the constructor of the class uses the
template argument, can the compiler just deduct the template argument type?
Is there any way to make this straighforward? Maybe using a Trait class?
-Charles
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sun Aug 07, 2005 1:39 am Post subject: Re: Elide template argument for object instantiation.... |
|
|
Charles Bissonnette wrote:
| Quote: | What I find redundant is to specifiy the template argument of the object
everytime I define an instance. Since the constructor of the class uses
the template argument, can the compiler just deduct the template argument
type?
Is there any way to make this straighforward? Maybe using a Trait class?
|
The usual workaround is to use a function creating such an object, see for
example the relationship between std::pair and std::make_pair.
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Aug 07, 2005 1:40 am Post subject: Re: Elide template argument for object instantiation.... |
|
|
Charles Bissonnette <chabiss (AT) hotmail (DOT) com> wrote:
| Quote: | I think this is just an annoyance of the language and I was wondering if
there's any workaround.
Supposed I have this object that I declare using a template class.
template<class _fun
class RangeFilter {
public:
RangeFilter(_fun fun) {...}
....
....
};
And then I used an instance of it :
RangeFilter
rf(std::ptr_fun(myptrFunction));
What I find redundant is to specifiy the template argument of the object
everytime I define an instance. Since the constructor of the class uses the
template argument, can the compiler just deduct the template argument type?
Is there any way to make this straighforward? Maybe using a Trait class?
if RangeFilter is used an argument to a function, you can write a |
helper fuction to construct it or sommthing like
tenpkate
RabgeFilter(F fun, boost::enable_if<boost::is_convertable_to<_fun,F>
| Quote: | ::type = 0))
{ /* ... */} |
template args may be in the wrong order, as I wrote it from memory.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
benben Guest
|
Posted: Sun Aug 07, 2005 10:42 am Post subject: Re: Elide template argument for object instantiation.... |
|
|
"Charles Bissonnette" <chabiss (AT) hotmail (DOT) com> wrote
| Quote: | I think this is just an annoyance of the language and I was wondering if
there's any workaround.
Supposed I have this object that I declare using a template class.
template<class _fun
class RangeFilter {
public:
RangeFilter(_fun fun) {...}
....
....
};
And then I used an instance of it :
RangeFilter
rf(std::ptr_fun(myptrFunction));
What I find redundant is to specifiy the template argument of the object
everytime I define an instance. Since the constructor of the class uses
the
template argument, can the compiler just deduct the template argument
type?
Is there any way to make this straighforward? Maybe using a Trait class?
-Charles
|
Your problem falls in two parts. You need to tell the compiler
(1) which template you are after, and
(2) what type you want to pass to the template.
You can expect formal language support in the future versions of C++
standard (C++ 0x), which might include typedef template, auto_type, type_of,
etc. However, in the current language you simply have to type the redundant
type name.
However, should the instance rf is only be passed to another function
template then you can relief the typing by creating a temporary, which
partially solves (1). Consider a function template that fiddles with all
kinds of range filters:
template
void use_rangefilter(RangeFilterT& rf)
{
// ...
}
In order to pass your RangeFilter<std::pointer_to_unary_function
| Quote: | instance to the above function, you can simply provide a helper function
to create a temporary, which partially solves (2): |
template <typename FunT>
RangeFilter<FunT> MakeRangeFilter(FunT& fun)
{
return RangeFilter<FunT>(fun);
}
// use range filter:
use_rangefilter(MakeRangeFilter(myPtrFunction));
// instead of
use_rangefilter(
RangeFilter<
std::pointer_to_unary_function
(myPtrFunction));
// or
use_rangefilter<RangeFilter<
std::pointer_to_unary_function >(
myPtrFunction);
Regards,
Ben
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|