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 / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Write a program to concatenate two strings.

585


Are strings immutable in c++?

656


Arrange Doubly linked list in the ascending order of its integral value and replace integer 5 with 7?

3702


What is the difference between containment and delegation?

704


Can you declare an array without a size in c++?

572






How do you find out if a linked-list has an end? (I.e. The list is not a cycle)

602


Is c++ used anymore?

590


What is doubly linked list in c++?

634


Am studying basic c++ programming, have been given the following assignment. Design a linear program to calculate the maximum stress a material can withstand given a force and a diameter of a circle. To find the required area pi should be defined. Have most of the program sorted out but am at a loss as to how to show the calculations required. Can anyone help?

1739


What is an orthogonal base class in c++?

659


What is a string example?

549


What is pointer -to-members in C++? Give their syntax?

597


Explain stack unwinding.

637


Explain the differences between list x; & list x();.

604


What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?

638