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

Why is it called c++?

587


Is multimap sorted c++?

559


What is the difference between the functions rand(), random(), srand() and randomize()?

730


What is increment operator in c++?

564


How compile and run c++ program in turbo c++?

633






Why struct is used in c++?

624


What it is and how it might be called (2 methods).

660


What is iomanip c++?

600


What does the linker do?

597


Explain what are single and multiple inheritances in c++?

561


why is c++ called oops? Explain

584


Describe linked list using C++ with an example.

651


What is the use of bit fields in structure declaration?

544


What is buffering in c++?

589


What is an adaptor class in c++?

608