top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to write function template in c++ ?

0 votes
236 views

Please provide some example for each .

posted Jul 22, 2014 by Maninder Bath

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Function templates means family of functions which can be used based on the data type -

Syntax
template < parameters > declaration

Sample Code

#include <iostream>

template<typename T>
void f(T s)
{
    std::cout << s;
}

int main()
{
    f<double>(1); // calls f<double>(double)
    f<>('a'); // calls f<char>(char)
    f(7); // calls f<int>(int)
    void (*ptr)(std::string) = f; // calls f<string>(string)
}
answer Jul 23, 2014 by Salil Agrawal
Similar Questions
0 votes

While doing design for a software, what things enforce to use template classes and template functions ?
Is it consider best practices or not ?

+8 votes

Write a thread safe data structure such that there could be only one writer at a time but there could be n readers reading the data. You can consider that incrementing or decrementing a variable is an atomic operation. If more than one threads try to write simultaneously then just select one randomly and let others wait.

...