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
Is java the same as c++?
Can a built-in function be recursive?
explain the reference variable in c++?
Can you please explain the difference between overloading and overriding?
How long it will take to learn c++?
What is the main use of c++?
Using a smart pointer can we iterate through a container?
What is auto used for in c++?
What is pointer with example?
Can a program run without main?
What is the role of copy constructor in copying of thrown objects?
What is a down cast?
what are the iterator and generic algorithms.
What is the c++ code?
Define token in c++.