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

1) Compiler will throw the error as C++ doesnt support +
operator for string. Another point is T c = Add
("SAM","SUNG"), where the function call assign to a
TEMPLATE object type, it syntactical error.

2)We need to write a seperate add function as given below

char* Add( const char *s1, const char*s2);

This will solve the problem

Is This Answer Correct ?    3 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are special characters c++?

550


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

604


What is a catch statement?

573


What are virtual constructors/destructors?

565


What are the five basic elements of a c++ program?

569






What is difference between c++ 11 and c++ 14?

560


What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

605


What do you mean by inheritance in c++?

585


Is map ordered c++?

580


Explain the concept of copy constructor?

607


What are the two shift operators and what are their functions?

547


What is the full form of ios?

543


What kind of problems can be solved by a namespace?

579


When one must use recursion function? Mention what happens when recursion functions are declared inline?

635


What is the difference between a reference and a pointer?

583