Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template parameter Deduction

Reply
Thread Tools

Template parameter Deduction

 
 
Neelesh
Guest
Posts: n/a
 
      11-10-2005
Hi all,
I had some confusion about deduction of non-type template parameters
for function templates :

template <class T, int i> void foo (T, int p = i) ;

void bar()
{
foo<int, 10>(40,40); // OK, T = int, i = 10
foo(40,40); // ERROR, T = int but i cannot be deduced.
}

However here I have another example :

template <class T, int i> void lookup ( Buffer<T,i> b);

void bar(Buffer<int, 128> b)
{
lookup(b,100); // No error, can infer T = int, i = 128
}

So it seems that when 'i' gets inferred as a "side effect" of type
matching, the compiler accepts that. However, it doesnot try to infer
it explicitly if required.

What does the standard say about this?

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      11-10-2005
Neelesh wrote:
> Hi all,
> I had some confusion about deduction of non-type template parameters
> for function templates :
>
> template <class T, int i> void foo (T, int p = i) ;
>
> void bar()
> {
> foo<int, 10>(40,40); // OK, T = int, i = 10
> foo(40,40); // ERROR, T = int but i cannot be deduced.
> }


The compiler cannot deduce non-type parameters for function templates
since there is no requirement that the parameters be compile-time
constants. Moreover, non-type template parameters are not all that
useful. Why call one routine when the parameter is 40 and a different
routine when it is 20? Aren't parameters supposed to be variables?
Can't one routine accept both a 20 or a 40 as a parameter?

Even if the routines should be distinct, they need not be function
templates. One function could be named Function20 and the other
Function40. Non-type function templates provide no more abstraction
than parameter-accepting functions, but can easily add significantly
more bloat to an application.

> However here I have another example :
>
> template <class T, int i> void lookup ( Buffer<T,i> b);
>
> void bar(Buffer<int, 128> b)
> {
> lookup(b,100); // No error, can infer T = int, i = 128
> }
>
> So it seems that when 'i' gets inferred as a "side effect" of type
> matching, the compiler accepts that. However, it doesnot try to infer
> it explicitly if required.


The compiler deduces that the type Buffer<int, 128> - so the 128 is
deduced from the type of the parameter. You want the compiler to deduce
40 from the value of the parameter - a completely different
proposition.

Greg

 
Reply With Quote
 
 
 
 
Neelesh
Guest
Posts: n/a
 
      11-10-2005

Greg wrote:

> The compiler deduces that the type Buffer<int, 128> - so the 128 is
> deduced from the type of the parameter. You want the compiler to deduce
> 40 from the value of the parameter - a completely different
> proposition.


Great. That last sentence just clears my confusion. Thanks a lot.

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      11-10-2005
"Greg" <> wrote in message
news: oups.com
>
> The compiler cannot deduce non-type parameters for function templates
> since there is no requirement that the parameters be compile-time
> constants.


That is news to me. See if you can get this to compile

template<int i>
void foo()
{
int x=i;
}
int a = 5;

int main()
{
foo<a>();
}

--
John Carson
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      11-10-2005
"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:dkves3$lfd$
> "Greg" <> wrote in message
> news: oups.com
>>
>> The compiler cannot deduce non-type parameters for function templates
>> since there is no requirement that the parameters be compile-time
>> constants.

>
> That is news to me. See if you can get this to compile
>
> template<int i>
> void foo()
> {
> int x=i;
> }
> int a = 5;
>
> int main()
> {
> foo<a>();
> }
>


Or perhaps I misunderstood you and you meant that in

template <class T, int i> void foo (T, int p = i) ;

the ordinary (not template) p parameter of foo doesn't have to be a compile
time constant.


--
John Carson

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function template type deduction from default parameter tobias.loew@steag.com C++ 2 01-08-2013 07:53 AM
function pointer as template parameter + type deduction er C++ 5 06-09-2009 02:04 AM
Turning off template parameter deduction KD C++ 4 06-20-2008 08:35 AM
template class instantiate without template parameter, automatic type deduction Fei Liu C++ 4 10-26-2007 02:39 PM
template class instantiate without template parameter, automatictype deduction Fei Liu C++ 0 10-25-2007 08:12 PM



Advertisments