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 / tathagata chakraborty

None of the other answers are coming to the point of this
question.They r all wrong.

right answer is the code will crash while returning frm Add
(). bcause the function is returning a local variable of
type T i.e. a+b as a refference. u cannont return a local
variable as refference bcause that will go out of scope as
soon as the function returns.

Is This Answer Correct ?    11 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain pass by value and pass by reference.

603


What is conditions when using boolean operators?

607


What do you mean by stack unwinding in c++?

738


What is std :: endl?

600


Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].

967






Can a constructor return a value?

582


What is the difference between the indirection operator and the address of oper-ator?

614


Differentiate between C and C++.

717


If a header file is included twice by mistake in the program, will it give any error?

559


When must you use a pointer rather than a reference?

610


What is the return value of the insertion operator?

609


How do you define a class in c++?

652


What is the size of a vector?

588


What is flush programming?

585


What is setiosflags c++?

539