473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Runtime type checking in a simple template function

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
11 5646
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
Why bother. Specialise or overload.

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

Thanks
Mar 18 '07 #3
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
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
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
"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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: George Sakkis | last post by:
Explicit type checking is not typically seen in Python code, and usually that's not a big problem; most typing errors are likely to raise a TypeError or AttributeError sooner than later. There are...
6
by: Web Developer | last post by:
Hi, I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks the "type", what more does it mean? ...
1
by: Patrick Kowalzick | last post by:
Hello all, I want to test run-time if a template function for a special type exists. See example below. The checkfoo function is where I need some hints. Regards, Patrick #include...
5
by: Big Blue Ox | last post by:
Hello, I am trying to store different types that are all derived from the same base class in a list template. So for instance lets say I have this is fragment of skeleton code: class Pet {...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
4
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
5
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.