semantics or real design issue?

C

ChasW

Consider the following 2 templates.

template <class T> T fromString (const std::string& s)
{
std::istringstream i (s);
T x;
i >> x;
return x;
}

template <class T> T fromString (T const &t, const std::string& s)
{
std::istringstream i (s);
T x;
i >> x;
return x;
}

As I see them, these are essentially the same with regards to how the
functions work, but the differences are: A) with the 2nd function
using the first parameter only for the purpose of deducing the type,
B) how both of the functions are called.

i.e.
The first might get called like this:
int n1 = fromString<int>(myStr);

and the second might get called like this:
double n2 = fromString (n, myStr);

the calling difference being that the first requires that a type be
cast to the function call so that the return type can be deduced,
whereas the second one deduces the type from the first parameter.


Is there anything functionally incorrect with the 2nd template?


(my thoughts)
My reason for even considering the 2nd template was to have a version
of the function that allows automatic type deduction.

This does not mean that I am oblivious to the type I am working with
and am unable to explicitly provide the template with a type, but it
does mean that it might be more convenient to just let the compiler
deduce the type since it is possible that the type might be an object
wich has a lengthy name or singularly exists in the absence of a
typedef.

Best Regards and thank you for any insightful replies,
ChasW
 
C

ChasW

deduce the type since it is possible that the type might be an object
wich has a lengthy name or singularly exists in the absence of a
typedef.

This was meant to say: ...object type which has a lengthy name...

ChasW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top