>>Concatenating two C-strings? That seems like it might be a reasonable specialization.
Perhaps, but it would hardly be a safe operation given the arguments passed to the function (no size parameter) unless the programmer knew for certain in advance that the second string would fit entirely inside the first - which is unlikely, given the widely generic nature of the function. If C++ strings were used, both would succeed anyway.
>>The issuing of double/float and int/short, etc could be readily taken care of by casting the smaller to the larger...
Touché.
Most of the programmers I've worked with tend to avoid macros of this type, and strongly favor other solutions.
Yes, as would I; but IMO it would be nearly as easy to just eliminate the function completely and code in the cout statement and addition manually. Perhaps even save a CPU cycle or two.
>>Type information is kept, and can be used appropriately.
Code:
template <typename T1, typename T2>
void sum(T1 a, T2 b)
{ std::cout << (a + b); }
#define SUM(a,b) {std::cout << ((a) + (b));}
struct Foobar {} foob;
...
sum("abcd", 1234);
SUM("abcd", 1234);
...
sum(4.5, foob);
SUM(4.5, foob);
In the first example, both give the same (wrong) output; in the second, they both give:
error C2677: binary '+' : no global operator found which takes type 'Foobar' (or there is no acceptable conversion)
The template function's error adds:
see reference to function template instantiation 'void sum<double,Foobar>(T1,T2)' being compiled
I suppose that argues in favour of the templated function, since the error gives more info, but the same information (or the important information) could readily have been deduced from the first error message anyway, especially since double-clicking the error (in MSVC) points you to the exact line causing the problem.