473,698 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template parameter Deduction

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?

Nov 10 '05 #1
4 2558
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

Nov 10 '05 #2

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.

Nov 10 '05 #3
"Greg" <gr****@pacbell .net> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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
Nov 10 '05 #4
"John Carson" <jc************ ****@netspace.n et.au> wrote in message
news:dk******** **@otis.netspac e.net.au
"Greg" <gr****@pacbell .net> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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

Nov 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2207
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call ambigous because of // odering deduction of the function signature,
3
1929
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
4
2577
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code, and the error I get: -------------- namespace LIST { template <typename T> struct Link; template <typename T> class List;
14
2895
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); template<typename T>
2
2370
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out exactly what the problem is. Is it related to the fact that a nested class is involved? #include <iostream> using std::cout;
7
1527
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
0
1242
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
3
3577
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
4
2540
by: KD | last post by:
I have a template function and I'm looking for a way to force the caller to specify the template parameters. In other words, I would like to turn off template parameter deduction. For example, template <class T> void foo(T t) { ... }
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.