Question
What's the syntax / semantics for a "function template"?

Answers

Consider this function that swaps its two integer arguments:

void swap(int& x, int& y)
{
int tmp = x;
x = y;
y = tmp;
}

If we also had to swap floats, longs, Strings, Sets, and FileSystems, we'd get pretty tired of coding lines that look almost identical except for the type. Mindless repetition is an ideal job for a computer, hence a function template:

template
void swap(T& x, T& y)
{
T tmp = x;
x = y;
y = tmp;
}

Every time we used swap() with a given pair of types, the compiler will go to the above definition and will create yet another "template function" as an instantiation of the above. E.g.,

int main()
{
int i,j; /*...*/ swap(i,j); // Instantiates a swap for int
float a,b; /*...*/ swap(a,b); // Instantiates a swap for float
char c,d; /*...*/ swap(c,d); // Instantiates a swap for char
std::string s,t; /*...*/ swap(s,t); // Instantiates a swap for std::string
...
}

Note: A "template function" is the instantiation of a "function template".   Your Comment




More Software Questions..
What is the inputsplit in map reduce software?

What is software configuration management?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?

What are the minimum system requirements to run Photoshop? Is it possible to run Photoshop over linux?

Which oracle package is used to manage the oracle lock management services?

What is Latch Up? Explain Latch Up with cross section of a CMOS Inverter. How do you avoid Latch Up?

What is marker interface?

What types of partitioning are there for BW?



Search
Can you Answer!!
  • Q The device which makes or breaks a circuit is?
  • Q out of 55 eggs 5 are defective. what is % of defective eggs
  • Q Who planted 'The 'free of Liberty at Seringapatam'?
  • Q Who against huge odds knocked defending champion John Higgins out of the 2010 World Snooker Championship?
  • Q Difference between ContentControl and ItemsControl?
  • Q Which committee, appointed by the Reserve Bank of India submitted its report on Capacity Building in banks and non-banks ?
  • Q Explain select function?
  • Q A sophomore is a student in which year of a US college?
  • Q What is the atrial heart rate in the setting of atrial fibrillation?
  • Q Which continent has the largest known deposits of coal?
  • Q How many logical drive is it possible to fit on to a physical disk?