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 / sg

"SAM" and "SUNG" will be considered as const char * and
there will be an compile time error. To over come this we
can call the fun as

string c = Add<string>("SAM","SUNG");

as the string class as '+' operator overloaded and it will
add the two string.

Is This Answer Correct ?    15 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between malloc()/free() and new/delete?

625


What is command line arguments in C++? What are its uses? Where we have to use this?

552


What is the most useful programming language?

586


Can a program run without main?

601


If a round rectangle has straight edges and rounded corners, your roundrect class inherits both from rectangle and from circle, and they in turn both inherit from shape, how many shapes are created when you create a roundrect?

638






We all know that a const variable needs to be initialized at the time of declaration. Then how come the program given below runs properly even when we have not initialized p?

556


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

2045


What is an iterator class in c++?

574


Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);

595


What is c++ redistributable?

601


How many namespaces are there in c++?

537


What is dev c++ used for?

579


What is the last index number in an array of 100 characters a) 100 b) 99 c) 101

566


Can I have a reference as a data member of a class? If yes, then how do I initialise it?

679


What can I use instead of namespace std?

615