Consider a c++ template funtion
template<class T>
T& Add(T a, T b){return a+b ;}

if this function is called as
T c = Add("SAM", "SUNG");

what will happen? What is the problem in the template
declaration/ How to solve the problem.

Answer Posted / mms zubeir

There are two things to be considered here.

First, T will be an unknown type in the place where we call
which gives a compilation error.

Second, if we call like this:

std::string c = Add("SAM", "SUNG"); or,
char* c = Add("SAM", "SUNG");

the compiler will convey an error since the arguments to
Add are interpretted as char pointers. The error may be
something like, "cannot add two pointers....".

Is This Answer Correct ?    4 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What relational operators if statements in c++?

646


Define Virtual function in C++.

625


What is an html tag?

632


Explain the difference between abstract class and interface in c++?

528


Is it possible to have a recursive inline function in c++?

554






What is an iterator?

690


In which header file does one find isalpha() a) conio.h b) stdio.h c) ctype.h

681


What are the comments in c++?

576


What is pointer -to-members in C++? Give their syntax?

597


Can c++ do everything c can?

601


What is the difference between the parameter to a template and the parameter to a function?

634


What header file is needed for exit(); a) stdlib.h b) conio.h c) dos.h

614


Write about the scope resolution operator?

618


What is the difference between an enumeration and a set of pre-processor # defines?

837


Explain abstraction.

619