Function template - Functions

Q.  In a program, If there exists a function template with two parameters and normal function say void add(int , int), so add(3,4) will _____________________ .
- Published on 19 Jul 15

a. Invoke function template body as it is generic one
b. Invokes normal function as it exactly matches with its prototype
c. Not be called and Compiler issues warning
d. Not be called and Compiler issues ambiguity in calling add()

ANSWER: Invokes normal function as it exactly matches with its prototype
 

    Discussion

  • Brijesh   -Posted on 08 Oct 15
    In a program, If there exists a function template with two parameters and normal function say void add(int , int), so add(3,4) will Invokes normal function as it exactly matches with its prototype
    Example:
    class Addition
    {
    public:
    template
    void Add(X a, X b)
    {
    X temp;
    temp=a+b;
    cout<<"Addition ="+temp;
    }
    void Add(int a, int b)
    {
    int temp;
    temp=a+b;
    cout<<"Addition ="< cout<<"Addition in normal function not in template function\n";
    }
    };
    int main()
    {
    Addition obj;

    int i=10, j=20;
    obj.Add(i, j);
    return 0;
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)