By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Runtime type checking in a simple template function

P: n/a
Dear all,

I have a simple template to convert its argument to a string:

template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.

Best regards,

Mar 18 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
utab wrote:
Dear all,

I have a simple template to convert its argument to a string:

template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.
Why bother. Specialise or overload.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 18 '07 #2

P: n/a
Why bother. Specialise or overload.

Hmm, I will read on template specialization. Is that right

Thanks
Mar 18 '07 #3

P: n/a
On 2007-03-18 17:58, utab wrote:
Dear all,

I have a simple template to convert its argument to a string:
By the way, regarding the subject of your post: templates are compile-
time so runtime typechecking won't do you any good since all the
decisions are already made when the app runs.

--
Erik Wikström
Mar 18 '07 #4

P: n/a
On 2007-03-18 17:58, utab wrote:
Dear all,

I have a simple template to convert its argument to a string:

template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.
Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typename T>
std::string AtoStr(T t){
std::ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecision(1) << t;
else
sstrm << t;
return sstrm.str();
}
int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";
}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t));
}

--
Erik Wikström
Mar 18 '07 #5

P: n/a
On Mar 18, 7:27 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-03-18 17:58, utab wrote:
Dear all,
I have a simple template to convert its argument to a string:
template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}
This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1
The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:
+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".
I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typename T>
std::string AtoStr(T t){
std::ostringstream sstrm;
sstrm << t;
return sstrm.str();

}

std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecision(1) << t;
else
sstrm << t;
return sstrm.str();

}

int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";

}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t));

}

--
Erik Wikström
Thanks

Mar 18 '07 #6

P: n/a
"Erik Wikström" <Er***********@telia.comwrote in message
news:Vv*******************@newsb.telia.net...
On 2007-03-18 17:58, utab wrote:
>Dear all,

I have a simple template to convert its argument to a string:

template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want all
you need to do is to fix the behaviour when the value is a double. This
you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typename T>
std::string AtoStr(T t){
std::ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)
Wouldn't this be better as:

if ( typeid( i ) == typeid( double ) )
sstrm << std::fixed << std::setprecision(1) << t;
else
sstrm << t;
return sstrm.str();
}
int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";
}

The if (i == t) business is just to check if the number is an integer (not
the type) in which case we don't need to print more than one 0 after the
dot. You might want to work a bit at how you want other doubles
represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t));
}

--
Erik Wikström

Mar 18 '07 #7

P: n/a
On Mar 19, 3:27 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-03-18 17:58, utab wrote:
Dear all,
I have a simple template to convert its argument to a string:
template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}
This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1
The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:
+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".
I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typename T>
std::string AtoStr(T t){
std::ostringstream sstrm;
sstrm << t;
return sstrm.str();

}

std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecision(1) << t;
else
sstrm << t;
return sstrm.str();

}

int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";

}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t));

}

--
Erik Wikström
Seems this is the right method.
You can make use of iomanip function to control the input and output.

Regards,
Sarath

http://sarathc.wordpress.com/

Mar 18 '07 #8

P: n/a
On Mar 19, 3:27 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-03-18 17:58, utab wrote:
Dear all,
I have a simple template to convert its argument to a string:
template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}
This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1
The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:
+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".
I am not so sure if output manipulators can help me.

Yes, since the bahaviour when the argument is an int is what you want
all you need to do is to fix the behaviour when the value is a double.
This you can do by proividing special double-version of the method:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>

template<typename T>
std::string AtoStr(T t){
std::ostringstream sstrm;
sstrm << t;
return sstrm.str();

}

std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)
sstrm << std::fixed << std::setprecision(1) << t;
else
sstrm << t;
return sstrm.str();

}

int main()
{
std::cout << AtoStr(1.0) << "\n";
std::cout << AtoStr(1.13456) << "\n";
std::cout << AtoStr("sdtfg") << "\n";

}

The if (i == t) business is just to check if the number is an integer
(not the type) in which case we don't need to print more than one 0
after the dot. You might want to work a bit at how you want other
doubles represented (number of digits and so on).

Notice that this does not work for floats. You can use this for floats:

std::string AtoStr(float t)
{
return AtoStr(double(t));

}

--
Erik Wikström
IMO, What Erik Said is the best solution.
You can make use of iomanip function to control the input and output.

Regards,
Sarath

http://sarathc.wordpress.com/

Mar 19 '07 #9

P: n/a
On 19 Mar, 00:19, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Erik Wikström" <Erik-wikst...@telia.comwrote in message
std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)

Wouldn't this be better as:

if ( typeid( i ) == typeid( double ) )
No, the above will always be false since i is an int. The purpose if
the above is to check if the value of t is an integer (not of type
integer) in which case we only want to add a decimal sign and one
zero. If it isn't we will use more precision.

--
Erik Wikström

Mar 19 '07 #10

P: n/a
"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11********************@d57g2000hsg.googlegrou ps.com...
On 19 Mar, 00:19, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Erik Wikström" <Erik-wikst...@telia.comwrote in message
std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)

Wouldn't this be better as:

if ( typeid( i ) == typeid( double ) )
- No, the above will always be false since i is an int. The purpose if
- the above is to check if the value of t is an integer (not of type
- integer) in which case we only want to add a decimal sign and one
- zero. If it isn't we will use more precision.

Yes, sorry, it should of been:

if ( typeid( t ) == typeid( int ))
Mar 20 '07 #11

P: n/a
On 20 Mar, 11:22, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Erik Wikström" <eri...@student.chalmers.sewrote in message

news:11********************@d57g2000hsg.googlegrou ps.com...
On 19 Mar, 00:19, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Erik Wikström" <Erik-wikst...@telia.comwrote in message
std::string AtoStr(double t){
std::ostringstream sstrm;
int i = t;
if (i == t)
Wouldn't this be better as:
if ( typeid( i ) == typeid( double ) )

- No, the above will always be false since i is an int. The purpose if
- the above is to check if the value of t is an integer (not of type
- integer) in which case we only want to add a decimal sign and one
- zero. If it isn't we will use more precision.

Yes, sorry, it should of been:

if ( typeid( t ) == typeid( int ))
It will still be false because t is a double since that's how it's
declared.

--
Erik Wikström

Mar 20 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.