473,657 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default arguements in Template

I have a small question w.r.t usage of default arguements in
template..
I shall try to elaborate this with an example..
let's say I have some template function , where EntryType is the input
for the template fn 1.. and another type where EntryType and lass P1
are both inputs..
case1 (1)template<cla ss EntryType_>
i.e void A<EntryType_>:: createObject(En tryType::inputI dType
inpId_ )..

case2 (2)EntryType and class P1 are the input for the template fn 1
template<class EntryType_, class P1>
i.e void A<EntryType_>:: createObject(En tryType::inputI dType
inpId_ )..
Inside this function, I call the class constructor based on the number
of input parameters passed ..
I have similar 25 functions(Funct ion overloading) where the difference
is going to be only in the number of arguements passed as input..(This
function is part of the base project)..
Now , for a new reqmt., I need to add a boolean to the create object
function..
It looks tedious for me to add the boolean in all the 25 template
functions as I feel the functionality is going to be the same in all
the 25 functions..
Ideally, what I would prefer is to have one single function with all
the parameters inside it..If I don't pass a parameter , let say in
case 2 of the above declaration , i should call case1
Normally, without the template this is possible with the usage of
default arguements as avbl in c++.
But for template this is not possible as the template function will
not know what kind of objects this is instantiating..

May 14 '07 #1
2 1811
If my explanation looks complicated, I can give u a small code
sample..
#include <iostream.h>
template <class A , class B>
void print(A a ,B b){ //Default arguement is
not possible
cout<<a<<endl;
cout<<b<<endl;
}
void print1(int a , int b=0){
cout<<a<<endl;
cout<<b<<endl;
};
int main()
{

print<int>(3,4) ;
//print<int>(3); --->This will throw an error as template will not
know the type
print1(3,4);
print1(3);
return 0;
}

How to have default arguements in templates???
I have ggiven an example of 2 arguements..But actually , I have 25
arguements..

May 14 '07 #2
"Nike" <pr*******@gmai l.comwrote in message
news:11******** *************@k 79g2000hse.goog legroups.com...
If my explanation looks complicated, I can give u a small code
sample..
#include <iostream.h>
template <class A , class B>
void print(A a ,B b){ //Default arguement is
not possible
cout<<a<<endl;
cout<<b<<endl;
}
void print1(int a , int b=0){
cout<<a<<endl;
cout<<b<<endl;
};
int main()
{

print<int>(3,4) ;
//print<int>(3); --->This will throw an error as template will not
know the type
print1(3,4);
print1(3);
return 0;
}

How to have default arguements in templates???
I have ggiven an example of 2 arguements..But actually , I have 25
arguements..
There is a proposal to allow default template arguments for function
templates in the new standard, but until then I guess you'd have to use
regular function overloading as a work-around

// general function
template<class A, class B, class Cvoid func(A a, B b, C c)
{
// ...
}

// overloads to simulate default arguments:
template<class A, class Bvoid func(A a, B b)
{
func(a, b, 23);
}

template<class Avoid func(A a)
{
func(a, "hi there", 23);
}

- Sylvester
May 14 '07 #3

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

Similar topics

14
5253
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while for keyword arguments the tutorial shows: def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
6
3346
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something like that possible? Some workaround? Thank you, Patrick
3
10038
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0" xmlns:ns1="abc" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" media-type="text/xml" standalone="yes" version="1.0"/> <xsl:template match="/">
2
4773
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7> class A { };
3
2122
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void bsort(T * si, T * ei, FUNCTOR cmpfunc) { int k = 0; for (T * i = si; i < ei - 1; i++, k++) for (T * j = si; j < (ei-k-1) ; j++)
2
3071
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node //Êý¾Ý±íÖнáµãÀàµÄ¶¨Òå
8
1859
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start (always going to be 2) and store them as characters. But I can't seem to get it to work because it is a pointer to a vector of characters. However, if I only run with integer arguements and use codeSnippet2 it works fine and they convert nicely to...
4
7231
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
8
284
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
8397
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
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6167
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
4158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2731
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
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.