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

What does std mean in c++?

573


What is the use of class in c++?

555


What is c++ coding?

640


What is the use of bit fields in structure declaration?

530


What is a stack c++?

561






How do I write a c++ program?

588


What is the full form of stl in c++?

664


Difference between delete and free.

601


Explain Memory Allocation in C/C++ ?

623


What are the four main data types?

574


Specify some guidelines that should be followed while overloading operators?

608


What is c++ and its uses?

608


Can I run c program in turbo c++?

568


Distinguish between a # include and #define.

636


What is the v-ptr?

627