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.

Answers were Sorted based on User's Feedback



Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

Answer / sg

"SAM" and "SUNG" will be considered as const char * and
there will be an compile time error. To over come this we
can call the fun as

string c = Add<string>("SAM","SUNG");

as the string class as '+' operator overloaded and it will
add the two string.

Is This Answer Correct ?    15 Yes 1 No

Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

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

Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

Answer / prasad

i will provide error.

declare template function as,
T Add(T a, T b){return a+b ;}

Is This Answer Correct ?    10 Yes 4 No

Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

Answer / skc

The error is adding Two pointers isn't alowed in C/C++.
The compiler imlicitly treats "SAM" as const char*. We need
to write a function with "explicit" keyword declaration like

explicit char * Add (const char* x1, const char* x2)
{
// check for null pointers.
// allocate strlen(x1)+strlen(x2)+1 using malloc
// say char*a1 = malloc...;
// check if malloc returns null..take corrective actions

// strcpy (a1, x1);
// strcat (a1, x2);
//strcat (a1, '\0');

return a1; // ask the caller to free the memory allocated
// for a1

}

since this function is writen explicit the compiler will
invoke this function and not call the default template
function.
this is what appears to me. haven't coded and verified.

// Regards, SADIQ

Is This Answer Correct ?    4 Yes 4 No

Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

Answer / pranay

MISTAKE:
1.returning a temp object so it goes out of scope.
2.using + for c-type strings.
SOLUTION:
string s = Add("SAM","SUNG");

Is This Answer Correct ?    1 Yes 1 No

Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

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

Consider a c++ template funtion template<class T> T& Add(T a, T b){return a+b ;} if t..

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

More C++ General Interview Questions

Which software is best for programming?

0 Answers  


What happens if an exception is throws from an object's constructor and from object's destructor?

3 Answers   TCS,


Read the following program carefully and write the output of the program. Explain each line of code according to given numbering. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> 1……………… int main (void) { pid_t pid; 2………………………… pid = fork(); 3…………………………. if (pid > 0) { int i; 4………………………… for (i = 0; i < 5; i++) { 5………………… …………… printf(" I AM VU : %d\n", i); 6………………… …………… sleep(1); } exit(0); } 7………………… ……… else if (pid == 0) { int j; for (j = 0; j < 5; j++) { 8……………………………… printf(" I have no child: %d\n", j); sleep(1); } _exit(0); } else { 9………………………………fprintf(stderr, "can't fork, error %d\n", errno); 10……………… … ………… exit (EXIT_FAILURE); } }

1 Answers  


What are put and get pointers?

0 Answers  


What is a manipulator in c++?

0 Answers  






What is the difference between a copy constructor and an overloaded assignment operator?

4 Answers   Belzabar, Citrix, Microsoft, Wipro,


which operator is used for performing an exponential operation a) > b) ^ c) none

0 Answers  


Is c the same as c++?

0 Answers  


When is a template better solution than a base class??

2 Answers   emc2,


What is class invariant?

1 Answers  


Do you know what is overriding?

0 Answers  


How do you remove an element from a set in c++?

0 Answers  


Categories